This is what I have so far:
myArray.map!{ rand(max) }
Obviously, however, sometimes the numbers in the list are not unique. How can I mak
Here is one solution:
Suppose you want these random numbers to be between r_min
and r_max
. For each element in your list, generate a random number r
, and make list[i]=list[i-1]+r
. This would give you random numbers which are monotonically increasing, guaranteeing uniqueness provided that
r+list[i-1]
does not over flowr
> 0For the first element, you would use r_min
instead of list[i-1]
. Once you are done, you can shuffle the list so the elements are not so obviously in order.
The only problem with this method is when you go over r_max
and still have more elements to generate. In this case, you can reset r_min
and r_max
to 2 adjacent element you have already computed, and simply repeat the process. This effectively runs the same algorithm over an interval where there are no numbers already used. You can keep doing this until you have the list populated.