From: Phrogz Date: 2008-01-16T00:19:57+09:00 Subject: Re: How to generate a serial random number On Jan 15, 6:43 am, Li Chen wrote: > I want to generate a serial of 5 random numbers based on the size of an > array(let say the size of array is 100). > Here are the rules:1) 5 random numbers are different 2) Once they are > generated they can't appear in the next round of random numbers. What is > the better way to do this? Now what I think of is to remove the items > corresponding to the random number from the previouse array and > eventually my array is smaller and smaller. Any other idea? I agree that removing the items is the way to go. To start with, create an array filled with unique numbers. Depending on your needs, you may want to just do something like: @array = (1..2000).map{ rand(max_rand_size) }.uniq You may end up with less then 2,000 values, depending on how big max_rand_size is. Then randomize the array. @array = @array.sort_by{ rand } Then just pop the values off the array each time you need one. value = @array.pop Here's a similar example I wrote in JavaScript for someone years ago: http://phrogz.net/tmp/7x7_random.html It differs in that it required a specific set of numbers in a random but non-repeating order.