0
$\begingroup$

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?

$\endgroup$
  • 1
    $\begingroup$ For this particular example, RandomInteger[{1, 5}, 10] would do. $\endgroup$ – Carl Woll Mar 1 '18 at 20:47
  • $\begingroup$ 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. $\endgroup$ – volcanrb Mar 1 '18 at 20:48
  • $\begingroup$ 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". $\endgroup$ – Henrik Schumacher Mar 1 '18 at 20:50
  • $\begingroup$ Is there a different function that is similar but which would evaluate each time? $\endgroup$ – volcanrb Mar 1 '18 at 20:53
  • 1
    $\begingroup$ You could use Table[test,10]. $\endgroup$ – Henrik Schumacher Mar 1 '18 at 20:57
2
$\begingroup$

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}}}

| improve this answer | |
$\endgroup$
1
$\begingroup$
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} $$

| improve this answer | |
$\endgroup$
0
$\begingroup$
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]
  ]
| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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