My objective is to be able to generate worksheets in categories like:

  1. Laws of Indices
  2. Simultaneous Linear equations and
  3. Quadratic equations

When I say generate is to generate for example 20 different problems. I believe I have addressed that with:

 \foreach \i in {1,...,#1}
 \item $my question type$

Somehow I believe that it is related to the question Creating random problems by using rand option.

The only difference is the restrictions that would be needed in code. For example, considering quadratic equations, there are three categories that is problems with equal roots, distinct and imaginary.

I am not an expert in pgf nor the random parameters. I am in the learning process here and I hope you the experts can assist me. I got the inspiration from Calculs et Programmation.

I haven't developed any code as yet but am ready to learn.


To better understand my question see the code below.

For example:


 % Une variable qui va servir

 % Génerer 6 entiers dans [-20,20]
 \reinitrand[first=-20, last=20, counter=a] \rand
 \chgrand[counter=b] \rand
 \chgrand[counter=c] \rand
 \chgrand[counter=d] \rand
 \chgrand[counter=e] \rand
 \chgrand[counter=f] \rand
 % Remplacer 0 par 1000

 % Un énoncé selon la valeur du dét
 {Expliquer pourquoi le système suivant 
 n'admet pas une unique solution :}%
 {Résoudre le système suivant :}
 \left\lbrace \begin{array}{rcl} 
 \thea x+\theb y &=& \thec \\
 \thed x+\thee y &=& \thef
 \end{array} \right.

 \randomsys \randomsys \randomsys \randomsys
 \randomsys \randomsys \randomsys \randomsys

The code above should generate random simultaneous equations. Thus my objective is to understand what each line is saying, an alternative to this like using the pgfmath from tikz, and what each definition in the code means.

Ultimately I want to learn how I can create random problems based on the above principle. The above topics mentioned (laws of indices, quad equations and simultaneous equations) are just samples to reference from.

  • 1
    This looks like quite an open-ended question. It isn't clear what would constitute a good answer to this question. Could you make it clearer what you are asking? A good start would be to explain why the answer you linked to doesn't solve your problem and to show us what you have tried so far.
    – Seamus
    Jan 24 '12 at 18:22
  • I want to learn how to use the pgf features or an explanation of these features and their implementation.
    – azetina
    Jan 24 '12 at 18:27

Without more details I don't know how to help any more than this. Here is an explanation of what is going on:

  1. I define \newcommand*{\Difficulty}{10}. This number is used to determine the range of the random numbers that are generated. In this case with it set to 10, the random numbers will be real numbers in the range 1...10.

  2. \pgfmathtruncatemacro is used to set a macro with the integer value of the computed expression. So \pgfmathtruncatemacro{\DenomDifficulty}{2*\Difficulty} set the macro \DenomDifficulty to be twice that of the value specified in \Difficulty. Similarily, \pgfmathtruncatemacro{\Neum}{random(\Difficulty)} set the macro \Neum to the value returned by random(\Difficulty). Since random() provides a real valued random number from 1..10 and (assuming here) we only want integer numbers, this necessitated the use of \pgfmathtruncatemacro, instead of the usual \pgfmathsetmacro.

  3. Similarly for the \LinearSystem and \QuadraticEquations macros, it is just a matter of generating more random numbers and using them in the math expressions.

  4. You mention in your question that there are three cases for quadratic: equal roots, distinct and imaginary. If you want to only generate equations sets of equations that have the same type of roots you would need to provide an algorithm for ensuring that type of root.

    If you do not require a restriction of integer values then this is fairly simple case. For example to get equal roots you could just randomly generate \A, \B and then use \C = (\B*\B)/(4*\A). However this won't necessarily be an integer, so you'd have to include extra logic here of that is desired.

enter image description here

Further Improvements:

  • Complete macros to generate only quadratic equations with distinct real and imaginary roots.
  • Generate negative numbers as well. In this case it would be advisable to include some logic so that we do not end up with + - as in the linked to example in the question. One way would be just to generate another random number from 0..1 and use a - sign instead of a + sign (or no sign as in the leading numbers) if the random number generated was greater than 0.6 (assuming you wanted approximately 40% of the questions to have negative signs, which also controls the level of difficulty dependent on the grade level).
  • Automatically convert decimals to reduced fractions. For this refer to this question from the future Random quadratic equation, which enhances the solution presented here.




    \foreach \i in {1,...,#1}{%
      \item $\dfrac{\Neum}{\Denom}$%

    \foreach \i in {1,...,#1}{%
      \item $\begin{cases}\begin{aligned}%
                \Xa x + \Ya y &= \Za \\%
                \Xb x + \Yb y &= \Zb \\%

    \foreach \i in {1,...,#1}{%
      \item $\A x^2 + \B x + \C = 0$%

    \foreach \i in {1,...,#1}{%
      \item $\A x^2 + \B x + \C = 0$%

\section{Random Fractions:}
\section{Random 2x2 Linear Equations:}
\section{Random Quadratic Equations:}
\section{Random Quadratic Equations (Equal Roots):}
  • 1
    Thanks for the quick reply. Your elaborate explanation does answer my question and does target my goal of understanding more the use of rand and pgfmath. I suppose that the restrictions can be set by using the appropriate mathematical concepts. I must reiterate that I want to understand how to typeset the code and what parameters I can play around with. Essentially, what can also help me is some literature or reference to some further reading. I don't know whether to say that this is for advanced users or not. Thanks a lot.
    – azetina
    Jan 24 '12 at 19:48
  • If something in my explanation is not clear let me know. As far as reading material: I'd recommend reading the TikZ & PGF manual. The only way to learn it is by using it. You should also separate the issues of using random numbers from typesetting the code. The solution here was in no way intended as an example of how to typeset this material, just on how to use random numbers. I just used what you had for typesetting and expanded on that. Jan 24 '12 at 20:38
  • I will take your advice seriously. I am still learning these stuff so technical help like what you provide does help me see and understand other perspective to issues that may arrise in the futre. I am glad with the effort you put in explaining the code.
    – azetina
    Jan 24 '12 at 22:22
  • @azetina learning "how to use the pgf features or an explanation of these features" ignores the sagetex package which gives access to a computer algebra system and Python. Sage can calculate the right answers and the code is easier to read. Example, here or here.
    – DJP
    May 14 '18 at 15:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.