1

I'm trying to make a randomly generated, say, square equations with integer coeffs. Like this 3x^2 - 6x + 8 = 0 But i want them to have integer roots too, so it would be way smarter to generate roots instead, and then produce coeffs. I came up with this

\pgfmathsetmacro{\x}{random(1,9)*((random(0,1)==0)?-1:1)}
\pgfmathsetmacro{\y}{random(1,8)*((random(0,1)==0)?-1:1)}
\pgfmathsetmacro{\a}{random(1,9)*((random(0,1)==0)?-1:1)}
\pgfmathsetmacro{\b}{-\a*(\x+\y)}
\pgfmathsetmacro{\c}{\a*\x*\y}

$$\a x^2 + \b x + \c = 0$$

Couple of problems here:

  1. It looks like 8.0 instead of 8.

  2. When it makes negative numbers, i get smth like + -9.0x instead of -9x. Can i generate random signs too? I tried it, but with no success.

  3. Obviously it would be better to have x instead of 1x. Is there a way to remove 1 if it gives me 1?
  4. Well, the major idea was, of course, to generate linear and quadratic equations and inequalities (with parametres, for ex. $4ax^2 -5x + 6a = 0 $. Since i don't quite understand this code Random quadratic equation can someone explain it or at least show how to remake it?

The ability to generate coefficients is important, since you can do all the things after that. Make stuff like $3sin(x)^2 - 5sinx + 8 = 0$

Andrew S.
  • 111
  • 2

1 Answers1

1

Here is a quick fix for some of the issues you raised.

\documentclass{article}
\usepackage{amsmath}
\usepackage{pgfplots}
\begin{document}
\pgfmathtruncatemacro{\x}{random(1,9)*((random(0,1)==0)?-1:1)}
\pgfmathtruncatemacro{\y}{random(1,8)*((random(0,1)==0)?-1:1)}
\pgfmathtruncatemacro{\a}{random(1,9)*((random(0,1)==0)?-1:1)}
\pgfmathtruncatemacro{\b}{-\a*(\x+\y)}
\newcommand{\myb}{\ifnum\b>0
+\b x
\else
\ifnum\b<0
\b x
\fi
\fi
}
\pgfmathtruncatemacro{\c}{\a*\x*\y}
\newcommand{\myc}{\ifnum\c>0
+\c
\else
\ifnum\c<0
\c 
\fi
\fi
}
\[\a x^2 \myb  \myc = 0\]
\end{document}

enter image description here

  • Thanks a lot man, that is indeed a huge improvement. – Andrew S. Jan 14 '18 at 20:23
  • @AndrewS. This is by no means comparable to the really nice code by percusse. To adjust it to your needs, at first approximation you may want to replace the 6 in \pgfmathrandominteger{\a}{\ifnum#1>1 1\else0\fi}{6} by something larger. However, I'd have to play with it in order to understand better what it does. –  Jan 14 '18 at 20:27
  • Yes, sadly I'm not even good at Latex programming, and the answer you showed is a black box thing for me. Would be really nice if someone explained it, or divided in parts. It is really hard to generalize his code. – Andrew S. Jan 14 '18 at 20:38
  • @AndrewS. Well, I do not believe that your question is a duplicate of Random quadratic equation since you employ much more sophisticated transformations to generate the coefficients. If the above code does the job for you, you may just go with it. (Of course, I might be considered biased. ;-) –  Jan 14 '18 at 20:42