# How to get a function within ConstantArray to evaluate for each list element?

Here is my code example:

ConstantArray[RandomInteger[{1, 5}], 10]

However as my output I get something like

{3, 3, 3, 3, 3, 3, 3, 3, 3, 3}


A more complex example:

test := Module[{a, b, c}, (a = RandomInteger[{1, 100}];
b = RandomInteger[{1, 100}]; c = RandomInteger[{1, 100}];
If[TrueQ[a < b] == True && TrueQ[b < c] == True, True, False])]


Which chooses 3 random integers a, b, and c and returns True if b is between a and c, False otherwise. However, plugging in

ConstantArray[test, 10]


just gives a list of 10 True's or 10 False's.

How can I construct a list for which a function like this is evaluated for every element in the list, rather than just evaluated once with that single output repeated? What function would work better than ConstantArray here?

• For this particular example, RandomInteger[{1, 5}, 10] would do. – Carl Woll Mar 1 '18 at 20:47
• I was just giving a simpler example to what I am actually trying to do. I will edit in the more complex one to be more clear. – volcanrb Mar 1 '18 at 20:48
• ConstantArray takes its first argument, evaluates it and copies the result into an array whose size is prescribed by the second argument. That's why the entries are not "random". – Henrik Schumacher Mar 1 '18 at 20:50
• Is there a different function that is similar but which would evaluate each time? – volcanrb Mar 1 '18 at 20:53
• You could use Table[test,10]. – Henrik Schumacher Mar 1 '18 at 20:57

You can use Table instead:

SeedRandom[1];
Tally @ Table[test, 100]


{{False, 88}, {True, 12}}

On the other hand, it would be faster to create a random matrix and then post-process. For example:

SeedRandom[1];
Tally[
Less @@@ RandomInteger[{1, 100}, {10^6, 3}]
] //AbsoluteTiming

SeedRandom[1];
Tally @ Table[test, 10^6] //AbsoluteTiming


{0.699966, {{False, 837811}, {True, 162189}}}

{7.77069, {{False, 837811}, {True, 162189}}}

a = RandomInteger[{1, 100}, {10}];
b = RandomInteger[{1, 100}, {10}];
c = RandomInteger[{1, 100}, {10}];
uff = Thread[#1 < #2 < #3 &[a, b, c]];
Grid@Transpose[{a, b, c , uff}] // TeXForm


gives:

$$\begin{array}{cccc} 46 & 93 & 92 & \text{False} \\ 32 & 70 & 12 & \text{False} \\ 89 & 37 & 63 & \text{False} \\ 4 & 7 & 73 & \text{True} \\ 47 & 67 & 61 & \text{False} \\ 90 & 11 & 79 & \text{False} \\ 9 & 55 & 24 & \text{False} \\ 95 & 14 & 10 & \text{False} \\ 60 & 91 & 17 & \text{False} \\ 79 & 33 & 68 & \text{False} \\ \end{array}$$

ClearAll[test]
test[n_] := Module[{
a = RandomInteger[{1, 100}, n],
b = RandomInteger[{1, 100}, n],
c = RandomInteger[{1, 100}, n]
},
MapThread[((#1 < #2) && (#2 < #3)) &, {a, b, c}]
]
test[10]


Of course in this particular case you could just

test[n_] := Module[{
ints = RandomInteger[{1, 100}, {3, n}]
},
MapThread[((#1 < #2) && (#2 < #3)) &, ints]
]