Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I made a function


that creates a random Polynomial of a certain degree, e.g.


now I want to create a Table of say 5 of those (different) Polynomials. A natural way would be

Table[makePolynomial, {5}]

but this creates 5 times the same Polynomial. How can I get a Table of 5 different Polynomials, that means makePolynomial is newly evaluated for each table item?

here is my makePolynomial function, I'm also looking for improvements:

makePolynomial[n_Integer, x_Symbol] := 
Module[{z, c}, z = RandomChoice[{-1, 1}] RandomInteger[{0, 100}];
c = Table[RandomInteger[{-100, 100}], {n}];
FromDigits[Reverse[AppendTo[c, z]], x]]
share|improve this question

closed as too localized by Yves Klett, Artes, Mr.Wizard May 13 '13 at 12:42

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

You should show your definition of makePolynomial. Most likely you need SetDeleyed i.e. := insted of Set (=). E.g. makePolynomial[n_] := x^Range[0, n - 1].RandomInteger[{-10, 10}, n], then it works. –  Artes May 13 '13 at 11:42
Since your given code works as you desire it to, and the problem was something outside of the question asked, I am closing this as "too localized" as it is unlikely to be of use to anyone else. –  Mr.Wizard May 13 '13 at 12:41

1 Answer 1

up vote 1 down vote accepted

I cannot give an explanation why it does not work for you unless you give the definition of your makePolynomial. But what about this?

makePolynomial[deg_] := Sum[RandomInteger[{0, 9}]*x^i, {i, 0, deg}]
Table[makePolynomial[3], {5}]
share|improve this answer
Thanks, I found the problem. If I make f=makePolynomial and then Table[f,{5}] I get the same Polynomial 5 times. I obviously need to put the makePolynomial directly in the Table argument –  Martin Koller May 13 '13 at 11:46
@MartinKoller You need not putting it there directly if you use ` f := makePolynomial ` –  Artes May 13 '13 at 11:59

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