Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select a random number a random number of times but never the same random number twice [duplicate]

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?

like image 361
William Stocks Avatar asked Nov 01 '12 15:11

William Stocks


People also ask

What happens if you use the random number multiple times in your program?

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.

How do you get a random number to not repeat in Python?

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.


1 Answers

As a quick one-liner:

rands = (0..10).to_a.shuffle[0, Random.rand(0..10)]
like image 152
Chowlett Avatar answered Sep 23 '22 02:09

Chowlett