# How do I create a list of 25 random quadratic equations in the form ax^2+bx+c?

My teacher suggests using the RandomInteger command along with the Factor command, but I cannot figure out the syntax. A, b, and c need to be non zero integers between -10 and 10.

-
Look at the documentation. You haven't explained what is your problem. Everyone has some problems with the syntax. – Artes Feb 11 '14 at 20:53
I looked at the documentation and understand how the RandomInteger command works, I'm just not sure where or how to apply it to the a, b, and c in the quadratic. – Kristie Feb 11 '14 at 21:02
Don't accept an answer so quickly. I was working on one of my own. – rcollyer Feb 11 '14 at 21:12
I didn't actually mean to, new to this! – Kristie Feb 11 '14 at 21:13

rand := RandomInteger[{-10, 9}] /. (0 -> 10)
eq := rand x^2 + rand x + rand
Array[eq &, {25}]


{5 - 3 x - 6 x^2, -9 - 6 x + 5 x^2, -2 - 6 x + 9 x^2, 7 - 6 x + 9 x^2, 2 + 8 x + 8 x^2, 3 - x + 5 x^2, -10 + 4 x - 5 x^2, -4 + 6 x + 10 x^2, 2 - 5 x - 5 x^2, -7 - 2 x - 8 x^2, -7 - 4 x - 6 x^2, -1 - 2 x + 7 x^2, 8 - 2 x + x^2, 5 + 2 x + 6 x^2, -8 + 3 x + 9 x^2, 2 - 2 x + 10 x^2, -9 + 2 x - 10 x^2, 7 + 9 x + 5 x^2, 4 - 5 x + 6 x^2, 8 - 6 x - 9 x^2, -1 + 7 x - 10 x^2, 4 - 6 x - 4 x^2, 5 - 3 x - 3 x^2, 2 + 9 x - 9 x^2, 1 - 3 x + 5 x^2}

Edit:

(1) If you want the order to be exactly a x^2 + b x + c, instead of c + b x + a x^2, you can do PolynomialForm[Array[eq &, {25}], TraditionalOrder -> True]

(2) By the way, one cannot write .../.0->10 . Otherwise Mathematica parse it as ... divided by 0.0

-
I would make one small change. I would use Array[eq&, {25}] instead. For the small list size, it won't matter, but for larger lists sizes, why build a list you are never going to use? – rcollyer Feb 11 '14 at 21:11
Thanks! That's true. – Yi Wang Feb 11 '14 at 21:16

Here's a slightly different method. Except for the fact that you want non-zero integers, RandomInteger would be perfect for this, e.g.

len = 5;
RandomInteger[{-10, 10}, {len, 3}]
(*
{{-3, -2, 10}, {2, -3, -8}, {5, 3, -2}, {9, 3, -2}, {-9, 3, -6}}
*)


which gives your list of triples directly. Now, you could alter the range and make substitutions like Yi Wang, but I think RandomChoice may be better, e.g.

RandomChoice[Range[10]~Join~(-Range[10]), {len, 3}]
(*
{{3, -3, -7}, {8, -1, -5}, {-2, -6, 5}, {-2, -4, 1}, {-8, 6, -8}}
*)


Then, constructing your polynomial is easy

#1 x^2 + #2 x + #3& @@@ RandomChoice[Range[10]~Join~(-Range[10]), {len, 3}]
(*
{-6 - 7 x + 7 x^2, -6 - 4 x - 4 x^2, -2 - 5 x + 3 x^2,
6 + x + 7 x^2, 4 + 10 x + 10 x^2}
*)


Obviously, increasing len to 25 for your application.

-

Here is a rough way to achieve this:

RandomPolynomial[max_Integer, var_Symbol] :=  Module[{pol, lis},
lis = Table[RandomChoice@Join[Range[-10, -1], Range[10]] var^deg, {deg, max,0, -1}]; pol = Tr@lis]


Now let's create 25 of these:

Table[RandomPolynomial[2, x], {25}]


-
RandomInteger works, except for the requirement that the coefficients are non-zero. – rcollyer Feb 11 '14 at 21:20
@rcollyer, Thanks, didn't catch that. – RunnyKine Feb 11 '14 at 21:21

This goofy way will let you specify how many to generate, the maximum power of x, and the +/- range of coefficients to use (excluding zero):

Plus @@@ Table[(RandomInteger[{-#3, #3 - 1}] /. (0 -> #3)) x^s, {#1}, {s, 0, #2}] &[5, 2, 10]
(*  {-4 + 10 x - x^2, -10 - 5 x + 6 x^2,  5 - 6 x + 10 x^2, -8 - 9 x - 5 x^2, -6 - 9 x - 5 x^2}  *)


Same result, more compact realization:

(RandomInteger[2 #3 - 1, {#, #2 + 1}] - #3 /. (0 -> #3)).x^Range[0, #2] &[5, 2, 10]


You can use //TraditionalForm on the output if you want it in decreasing exponent order.

Credit to Yi Wang for zero elimination idea.

-