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?
Thanks
The first argument to
Arrayis intended to be a function which is applied to each array index (in this case, the values 1 through 7). If you evaluateThe result is something like this:
What has happened is this:
Random[Real, {-10, 10}]is evaluated producting3.91766.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:
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:In current versions of Mathematica, the
Randomfunction is obsolete and has been replaced byRandomIntegerandRandomReal. In this case,RandomRealis useful:… 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.