Take the 2-minute tour ×

I am attempting to generate an array of random numbers in Mathematica in a functional manner. This is my current attempt so far:

Array[Random[Real, {-10, 10}], 7]

The problem is that this is printing always the same number, which, of course, is not what I am looking for. I do understand that Mathematica is evaluating Random[Real, {-10,10}] once and then using always the same value. How can I circumvent the problem, keeping this functional style?


share|improve this question
Second comic: search.dilbert.com/comic/Random%20Number. Sorry. Just had to. –  Bair Aug 19 '11 at 3:50
+1 for meeting the situation perfectly... –  PlagueEditor Aug 19 '11 at 4:02

3 Answers 3

up vote 14 down vote accepted

The first argument to Array is intended to be a function which is applied to each array index (in this case, the values 1 through 7). If you evaluate


The result is something like this:

{3.91766[1], 3.91766[2], 3.91766[3], 3.91766[4], 3.91766[5], 
 3.91766[6], 3.91766[7]}

What has happened is this:

  1. Random[Real, {-10, 10}] is evaluated producting 3.91766.
  2. This result is used as a "function" and is applied to each array index.

Thus we see a list of expressions like 3.91766[1], an odd expression where a real number is being applied like a function to an integer.

If the intent is generate a list of 7 different random numbers, one could use:

Array[Random[Real, {-10, 10}] &, 7]

The only difference between this and the original expression is the presence of '&'. This makes the first argument a function. The function is applied to each array index (but ignores it), and then returns a new random number each time.

An alternative way to obtain this result uses Table:

Table[Random[Real, {-10, 10}], {7}]

In current versions of Mathematica, the Random function is obsolete and has been replaced by RandomInteger and RandomReal. In this case, RandomReal is useful:

RandomReal[{-10, 10}, 7]

... where the first argument is the range to choose from and the second argument is the number of desired values. Note that higher dimensional random arrays can also be generated, e.g.

RandomReal[{-10, 10}, {7, 7}]
share|improve this answer

You could use Array[Random[Real, {-10, 10}] &, 7]. Note the ampersand, which basically turns the first argument into a function with no arguments.

But WReach's methods are far superior...

share|improve this answer
WReach's answer has the same code verbatim: Array[Random[Real, {-10, 10}] &, 7] –  Mr.Wizard Aug 19 '11 at 4:25
@Mr.Wizard Despite appearances, this is just another case of simultaneous answers. My original response did not contain this expression although I thought of it immediately after I hit 'save'. I added it right away, within the time window where edits go unrecorded, and by the time I saved my updated response Brett had contributed his. –  WReach Aug 19 '11 at 4:50
@WReach then I made yet another useless comment. I shall try to make less of those. –  Mr.Wizard Aug 19 '11 at 5:51

I note that I'm not 100% profficient at this... but I've got some links that appear to help. The following seems to work for me. The numbers being (1.) The minimum (2.) The Maximum and (3.) The number of numbers to make.


Take from here: here. Hope that helps!

share|improve this answer

Your Answer


