Possible Duplicate:
How do I generate a list of n unique random numbers in Ruby?
I want to do:
Random.rand(0..10).times do
puts Random.rand(0..10)
end
with the exception that if a random number has already been displayed it cannot be displayed again. How do I do this most easily?
If you use randomNumber() multiple times in your program it will generate new random numbers every time. You can think of each randomNumber() like a new roll of a die.
Algorithm (Steps)Create an empty list which is the resultant random numbers list. Use the for loop, to traverse the loop 15 times. Use the randint() function(Returns a random number within the specified range) of the random module, to generate a random number in the range in specified range i.e, from 1 to 100.
As a quick one-liner:
rands = (0..10).to_a.shuffle[0, Random.rand(0..10)]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With