1

I'm trying to do the following:

R = {a, b, c, d, e, f, g, h, i};
Table[R[[n]] = RandomInteger[{0, 20}], {n, 1, Length[R]}]

My intention is to assign a random value to each of these variables. But somehow it's not working. What else do I need to do? I've tried to do:

ToExpression[R[[n]] = RandomInteger[{0, 30}]]

And:

Evaluate[R[[n]] = RandomInteger[{0, 30}]]
Red Banana
  • 5,329
  • 2
  • 29
  • 47

1 Answers1

7

Remember that = is really Set, and Set is a pretty ordinary function. Also, RandomInteger can make a list all by itself, so you don't need Table.

Edit 11/15: Jerry Guern pointed out the need to be careful with quoting to have it work repeatedly. Here, I use Unevaluated to quote each variable:

r = Unevaluated /@ {a, b, c, d, e, f, g, h, i};

Now, you may use r repeatedly as input to:

MapThread[Set, {r, RandomInteger[{0, 20}, Length[r]]}];
John Doty
  • 13,712
  • 1
  • 22
  • 42