# How to generate a serial random number

**URL:** <https://www.ruby-forum.com/t/how-to-generate-a-serial-random-number/196573>\
**Category:** Ruby\
**Created:** [January 15, 2008, 3:43pm UTC](https://www.ruby-forum.com/t/how-to-generate-a-serial-random-number/196573 "2008-01-15T15:43:45Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [January 15, 2008, 3:43pm UTC](https://www.ruby-forum.com/t/how-to-generate-a-serial-random-number/196573/1 "2008-01-15T15:43:45Z")

</div>

Hi all,

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?

Thanks,

Li

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [January 15, 2008, 4:22pm UTC](https://www.ruby-forum.com/t/how-to-generate-a-serial-random-number/196573/2 "2008-01-15T16:22:41Z")

</div>

On Jan 15, 6:43 am, Li Chen [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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](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.

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [January 15, 2008, 4:26pm UTC](https://www.ruby-forum.com/t/how-to-generate-a-serial-random-number/196573/3 "2008-01-15T16:26:49Z")

</div>

On Jan 15, 7:16 am, Phrogz [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> You may end up with less then 2,000 values…

5.times{  
me.stare\_at( :word=\>“then” )

me.stare\_at( :object=\>“fingers”, :using=\>:horror, :modifier=\>:increasing  
)  
}

me.rename( :object=\>“fingers”, “homonymic transcription devices” )

me.puts “Ugh. I _hate_ it when other people type that, too.”

me.last\_post.gsub ‘less then’, ‘less than’

me.sigh

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [January 15, 2008, 7:00pm UTC](https://www.ruby-forum.com/t/how-to-generate-a-serial-random-number/196573/4 "2008-01-15T19:00:38Z")

</div>

How “random” does random have to be?

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [January 15, 2008, 5:49pm UTC](https://www.ruby-forum.com/t/how-to-generate-a-serial-random-number/196573/5 "2008-01-15T17:49:17Z")

</div>

Phrogz \<phrogz [mac.com](http://mac.com)\> writes:

> On Jan 15, 7:16 am, Phrogz [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > You may end up with less then 2,000 values…  
> > …  
> > me.last\_post.gsub ‘less then’, ‘less than’

If you want to be picky then it should be “fewer” too

You have “fewer” things you can have “a few” of  
~~ fewer/a few values NOT fewer/a few time

You have “less” things you can have “a little” of  
~~ less/a little time NOT less/a little values

But then you still have a “than” to deal with

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [January 16, 2008, 6:27pm UTC](https://www.ruby-forum.com/t/how-to-generate-a-serial-random-number/196573/6 "2008-01-16T18:27:14Z")

</div>

Hi,

Thank you all for the inputs and the ideas.

Li
