How to generate a serial random number

Discussion in 'Ruby' started by Li Chen, Jan 15, 2008.

  1. Li Chen

    Li Chen Guest

    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
    --
    Posted via http://www.ruby-forum.com/.
    Li Chen, Jan 15, 2008
    #1
    1. Advertising

  2. Li Chen

    Phrogz Guest

    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.
    Phrogz, Jan 15, 2008
    #2
    1. Advertising

  3. Li Chen

    Phrogz Guest

    [OT] Re: How to generate a serial random number

    On Jan 15, 7:16 am, Phrogz <> wrote:
    > You may end up with less then 2,000 values...


    5.times{
    me.stare_at( :word=>"then" )

    me.stare_at( :eek:bject=>"fingers", :using=>:horror, :modifier=>:increasing )
    }

    me.rename( :eek:bject=>"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
    Phrogz, Jan 15, 2008
    #3
  4. Li Chen

    Gareth Adams Guest

    Re: [OT] Re: How to generate a serial random number

    Phrogz <phrogz <at> mac.com> writes:

    > On Jan 15, 7:16 am, Phrogz <> 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
    Gareth Adams, Jan 15, 2008
    #4
  5. Re: [OT] Re: How to generate a serial random number

    How "random" does random have to be?
    Paul Stickney, Jan 15, 2008
    #5
  6. Li Chen

    Li Chen Guest

    Li Chen, Jan 16, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. C T
    Replies:
    3
    Views:
    2,429
    Charles M. Elias
    Mar 5, 2004
  2. Matt
    Replies:
    24
    Views:
    9,362
    Malcolm
    Oct 18, 2004
  3. RayKnight

    Generate a random number

    RayKnight, Apr 29, 2005, in forum: Java
    Replies:
    2
    Views:
    326
    Peter MacMillan
    Apr 29, 2005
  4. globalrev
    Replies:
    4
    Views:
    757
    Gabriel Genellina
    Apr 20, 2008
  5. VK
    Replies:
    15
    Views:
    1,161
    Dr J R Stockton
    May 2, 2010
Loading...

Share This Page