On Mon, Apr 21, 2008 at 6:06 PM, Daniel Finnie <dan / danfinnie.com> wrote: > Hi Todd, > > I like your math-y solution -- it probably runs circles around the > non-mathy ones. > > For generating a random array of numbers, I like to use this technique: > > > > >> Array.new(5) { rand(100) } > => [47, 78, 88, 39, 61] > >> Array.new(5) { rand(100) } > => [48, 38, 33, 94, 98] > > So you could do this to generate an array of arrays like the one in your post: > > >> require 'pp' > => false > >> def rand_ar > >> Array.new(10) do > ?> Array.new(3) do > ?> Array.new(3) { rand(100) - 50 } > >> end > >> end > >> end > => nil > >> pp rand_ar > [[[47, 14, 20], [1, -18, -15], [7, -46, -44]], > [[-5, -44, -32], [-40, 9, 1], [16, 16, 10]], > [[-15, -13, 2], [16, -16, 37], [-1, 17, -4]], > [[-13, 5, -31], [47, -30, -27], [13, -2, -16]], > [[23, -50, 12], [3, 34, 6], [16, 24, -34]], > [[-6, -19, -25], [21, 5, -47], [-38, -7, -13]], > [[13, 48, 23], [12, -33, -7], [48, -14, -47]], > [[-12, -28, -31], [3, 37, -16], [-50, 29, -44]], > [[13, -21, 29], [36, 30, 45], [8, 9, 36]], > [[-16, 8, -47], [43, -49, 42], [45, 4, 5]]] > => nil > >> pp rand_ar > [[[45, 5, 34], [19, -14, 11], [-17, 32, -43]], > [[-2, 31, 40], [-16, 40, -19], [-10, -18, 37]], > [[21, 38, -13], [47, 23, -10], [0, -2, -12]], > [[7, 49, 8], [-23, -38, -25], [-45, -31, -3]], > [[47, -20, -34], [39, -21, -35], [17, 17, -6]], > [[0, 24, -2], [-38, 14, -26], [4, 2, -9]], > [[47, 7, -34], [28, -24, 18], [-9, -5, -6]], > [[-34, -15, 1], [-26, 17, -2], [-8, 46, 30]], > [[-20, 23, -28], [44, -12, -18], [8, -40, -47]], > [[31, -6, 32], [5, 27, 31], [-14, -14, -41]]] > > Dan Hey Dan, Thanks for pointing that out! I keep forgetting that Array.new takes a block, which is even more embarrassing, because I have in fact suggested that solution to someone else in the past. Todd