[Tex/LaTex] How to create a random math problem in LaTeX

random numbers

I want to create a math problem-solution sheet. The coefficients a, b, c must be assigned on the fly at random so the generated pdf will show a numeric quadratic equation rather than a symbolic quadratic equation. Note a must not be zero.

\documentclass{article}
\begin{document}
\section{Problem}
Let \(x_1\) and \(x_2\) be the roots of \(a x^2 +bx +c=0\). Find  \(x_1x_2\).

\section{Solution}
\[
x_1x_2=\frac{c}{a}
\]
\end{document}

Please kindly make the solution get automatically simplified. For example: \frac{c}{a}=3 when a=2 and c=6.


Actually the real case is more complex than this one. Not only the equations must be generated at random, but also the associated graphic plots that I write in PSTricks.


I chose Paulo's answer because using Maxima as the computation engine is very very great idea. Other answers are also good and creative, especially Altermundus'.

Best Answer

This is a partial solution and probably it's not what you want. However, I'm posting it solely for record purposes, in case anyone would need it. =)

I'll use Maxima:

Maxima is a system for the manipulation of symbolic and numerical expressions, including differentiation, integration, Taylor series, Laplace transforms, ordinary differential equations, systems of linear equations, polynomials, and sets, lists, vectors, matrices, and tensors. Maxima yields high precision numeric results by using exact fractions, arbitrary precision integers, and variable precision floating point numbers. Maxima can plot functions and data in two and three dimensions.

First of all, I created a script file named solve.cmd in my working directory:

@echo off
rem If Maxima is not in your path, you need to set it.
rem Also, I'm making use of the 'head' command, you can
rem obtain the Win32 version easily with MSys.
set PATH=%PATH%;"C:\Program Files\Maxima-5.24.0\bin"
maxima --very-quiet -r %1 | head --lines=-1

I'm not in my Linux box right now, but a .sh file is similar.

Using Jake's code, I added a call to this script:

\documentclass{article}
\usepackage{pgf}

\begin{document} 

\section{Problem}

\pgfmathsetseed{1}
\pgfmathtruncatemacro\coeffa{random(2,10)}
\pgfmathtruncatemacro\coeffb{random(2,10)}
\pgfmathtruncatemacro\coeffc{random(2,10)}

% call to the 'solve' script providing the coefficients.
% Maxima's 'solve' function will return x1 and x2, and the
% 'tex' function will convert the answer to TeX. The last
% line of the output will be removed (the 'head' command)
% because Maxima adds a 'false' line to the end of the file.
% Then we redirect the output to a file.
\immediate\write18{solve.cmd "tex(solve(\coeffa*x^2 + (\coeffb*x) + (\coeffc)));" > solution1.tex}

Let \(x_1\) and \(x_2\) be the roots of \(\coeffa x^2 +\coeffb x +\coeffc=0\). Find  \(x_1x_2\).

\section{Solution}

% read the solution
\input solution1

\end{document}

I tried to use Maxima's plot2d function together with the call, but I failed to fix a minor error with file extensions. I intended to create a png plot with gnuplot and include it. Sorry, it didn't work yet.

Don't forget to use the --shell-escape or --enable-write18 flag.

EDIT: It gave me a tremendous headache, but I guess I found a nice improvement. =)

I'm not used to Maxima, though I like to use it for simple calculations. If my code is somehow incorrect, please help me improve it. Thanks.

I edited the solve.cmd file in my working directory to look like this:

@echo off
rem If Maxima is not in your path, you need to set it.
rem Now, I don't use 'head' anymore, but 'grep'. The idea
rem behind this code is:
rem   1. generate the three coefficients,
rem   2. output both equation and solution to a file,
rem   3. get rid of the 'false' lines in the text (Maxima's fault)
rem      (we do this thanks to 'grep' using an inverse flag)
rem   4. direct output to a file 'problem.txt'.
@echo off
set PATH=%PATH%;"C:\Program Files\Maxima-5.24.0\bin"
maxima --very-quiet -r "s: make_random_state(true)$ set_random_state(s)$ fullrand(low, high) := floor(random(1.0) * ( 1 + high - low)) + low$ a : fullrand(-10,10)$ s: b : fullrand(-10,10)$ c : fullrand(-10,10)$ tex(a*x^2 + b*x + c); tex(solve(a*x^2 + b*x + c));" | grep -v false > problem.txt

Now the tricky part, the LaTeX code:

\documentclass{article}

\immediate\write18{solve.cmd}

\newread\myread
\openin\myread=problem.txt
\read\myread to \theequation
\read\myread to \thesolution

\begin{document}

\section{Problem}

This is our equation:

\theequation

\section{Solution}

And this is our solution:

\thesolution

\end{document}

Let's check the code: first of all, we run solve.cmd, which outputs both equation and solution to a file named problem.txt. Then we create a \newread\myread and open this file \openin\myread=problem.txt. Now we map the first line to \theequation (which is the equation which we defined in our Maxima code) and the second line to \thesolution (same logic). Then we call it when needed.

Again, probably some of my Maxima code is faulty. But I hope you get the idea behind this. In case of changing the equation or solution to a inline version, we may probably do some sed in it and replace the pattern. =)

Related Question