random number generator

Discussion in 'C++' started by Alan Lee, Apr 7, 2004.

  1. Alan Lee

    Alan Lee Guest

    Is there any way to set RAND_MAX in rand()? I am looking for a way to get a
    random number between 1 and 1E9. So far I can't figure out how to do that.
    any suggestions would be greatly appreciated.
     
    Alan Lee, Apr 7, 2004
    #1
    1. Advertising

  2. "Alan Lee" <> wrote in message
    news:c503fk$p4j$...
    > Is there any way to set RAND_MAX in rand()? I am looking for a way to get

    a
    > random number between 1 and 1E9. So far I can't figure out how to do

    that.
    > any suggestions would be greatly appreciated.
    >


    No there is no way to set RAND_MAX.

    There are two ways to go, you can either write a random number generator
    from scratch. There must be tons of literature on the web on how to do this.

    Or you have to call rand() several times to generate your large random
    numbers.

    On my system RAND_MAX is 0x7FFF which means that my random number generator
    produces 16 bit random numbers. You need 1 to 1E9 which is at least 32 bits.
    So if I call rand() twice (and I do it in the right way) I can produce a 32
    bit random numbers.

    The C FAQ has a few questions on random number generators (although not your
    specific one). Have a look http://www.eskimo.com/~scs/C-faq/s13.html and
    post back here if you are still stuck.

    john
     
    John Harrison, Apr 7, 2004
    #2
    1. Advertising

  3. Alan Lee

    David Harmon Guest

    On Tue, 6 Apr 2004 22:27:15 -0700 in comp.lang.c++, "Alan Lee"
    <> wrote,
    >Is there any way to set RAND_MAX in rand()?


    No. RAND_MAX is there to tell _you_ what the behavior of rand() is.
    You cannot change it.

    > I am looking for a way to get a
    >random number between 1 and 1E9. So far I can't figure out how to do that.
    >any suggestions would be greatly appreciated.


    Why? The answer to that will make a great difference in what it takes
    to satisfy you. I guess your requirements greatly exceed rand()'s
    promises. I suppose you need a professional grade random number
    generator, perhaps from the cryptologists.

    In any case, read the references mentioned in Q 13.5 of Steve Summit's C
    FAQ. It is always good to check the FAQ before posting. You can get
    the FAQ at:
    http://www.eskimo.com/~scs/C-faq/top.html
     
    David Harmon, Apr 7, 2004
    #3
  4. >
    > Or you have to call rand() several times to generate your large random
    > numbers.
    >


    As David suggests, if you do this your random numbers will be of lower
    quality.

    john
     
    John Harrison, Apr 7, 2004
    #4
  5. Alan Lee

    Alan Lee Guest

    For now, I don't care too much for the quality as long as its still a random
    number generator. I will try to find a higher quality generator once I
    finish my program but I just would like to test it for now. How do I get a
    larger number by stringing calls to rand together?


    "David Harmon" <> wrote in message
    news:...
    > On Tue, 6 Apr 2004 22:27:15 -0700 in comp.lang.c++, "Alan Lee"
    > <> wrote,
    > >Is there any way to set RAND_MAX in rand()?

    >
    > No. RAND_MAX is there to tell _you_ what the behavior of rand() is.
    > You cannot change it.
    >
    > > I am looking for a way to get a
    > >random number between 1 and 1E9. So far I can't figure out how to do

    that.
    > >any suggestions would be greatly appreciated.

    >
    > Why? The answer to that will make a great difference in what it takes
    > to satisfy you. I guess your requirements greatly exceed rand()'s
    > promises. I suppose you need a professional grade random number
    > generator, perhaps from the cryptologists.
    >
    > In any case, read the references mentioned in Q 13.5 of Steve Summit's C
    > FAQ. It is always good to check the FAQ before posting. You can get
    > the FAQ at:
    > http://www.eskimo.com/~scs/C-faq/top.html
    >
    >
     
    Alan Lee, Apr 7, 2004
    #5
  6. "Alan Lee" <> wrote in message
    news:c505k5$qoq$...
    > For now, I don't care too much for the quality as long as its still a

    random
    > number generator. I will try to find a higher quality generator once I
    > finish my program but I just would like to test it for now. How do I get

    a
    > larger number by stringing calls to rand together?
    >


    For instance, to get a 32 bit random number from 2 16 bit random numbers

    (rand() << 16)|rand()

    This assumes that int is at least 32 bits, otherwise you'll have to add some
    casts.

    john

    >
    > "David Harmon" <> wrote in message
    > news:...
    > > On Tue, 6 Apr 2004 22:27:15 -0700 in comp.lang.c++, "Alan Lee"
    > > <> wrote,
    > > >Is there any way to set RAND_MAX in rand()?

    > >
    > > No. RAND_MAX is there to tell _you_ what the behavior of rand() is.
    > > You cannot change it.
    > >
    > > > I am looking for a way to get a
    > > >random number between 1 and 1E9. So far I can't figure out how to do

    > that.
    > > >any suggestions would be greatly appreciated.

    > >
    > > Why? The answer to that will make a great difference in what it takes
    > > to satisfy you. I guess your requirements greatly exceed rand()'s
    > > promises. I suppose you need a professional grade random number
    > > generator, perhaps from the cryptologists.
    > >
    > > In any case, read the references mentioned in Q 13.5 of Steve Summit's C
    > > FAQ. It is always good to check the FAQ before posting. You can get
    > > the FAQ at:
    > > http://www.eskimo.com/~scs/C-faq/top.html
    > >
    > >

    >
    >
     
    John Harrison, Apr 7, 2004
    #6
  7. Alan Lee

    David Harmon Guest

    On Tue, 6 Apr 2004 23:03:48 -0700 in comp.lang.c++, "Alan Lee"
    <> wrote,
    >For now, I don't care too much for the quality as long as its still a random
    >number generator. I will try to find a higher quality generator once I
    >finish my program but I just would like to test it for now. How do I get a
    >larger number by stringing calls to rand together?


    Don't top post.

    Follow the advice you just read in the FAQ I pointed you at. You did
    read it, didn't you?
     
    David Harmon, Apr 7, 2004
    #7
  8. Alan Lee wrote:

    > For now, I don't care too much for the quality as long as its
    > still a random
    > number generator. I will try to find a higher quality generator
    > once I
    > finish my program but I just would like to test it for now. How
    > do I get a larger number by stringing calls to rand together?
    >


    If you really don't care, why not do it like this:

    rand()*(1e9/RAND_MAX)

    Bernhard
     
    Bernhard Holzmayer, Apr 7, 2004
    #8
  9. > > How do I get a larger number by stringing calls to rand together?
    > For instance, to get a 32 bit random number from 2 16 bit random numbers
    > (rand() << 16)|rand()
    > This assumes that int is at least 32 bits, otherwise you'll have to add

    some
    > casts.


    If RAND_MAX is 0x7fff as in John Harrisons example, the formula above will
    not work as wanted.
    rand()*(RAND_MAX+1)+rand() is better in that case.

    How about using floating point:

    double n1=double(rand())/(RAND_MAX+1) will result in a number between 0 and
    1 (1 not included).

    The "range" can then be increased by:

    double n2=(rand()+n1)/(RAND_MAX+1);

    This can be repeated until the number of bits are achieved:

    double n3=(rand()+n3)/(RAND_MAX+1);

    This would give 45 bits if RAND_MAX is 15 bits.
    Then finally you can map this into the range you want:

    int r=int(n3*1e9)+1;

    Niels Dybdahl
     
    Niels Dybdahl, Apr 7, 2004
    #9
  10. >
    > If RAND_MAX is 0x7fff as in John Harrisons example, the formula above will
    > not work as wanted.
    > rand()*(RAND_MAX+1)+rand() is better in that case.
    >


    My brain can't have been functioning correctly when I posted that. But

    rand()*(RAND_MAX+1)+rand()

    is only 30 bit random number, assuming rand() is fifteen bits.

    > How about using floating point:
    >
    > double n1=double(rand())/(RAND_MAX+1) will result in a number between 0

    and
    > 1 (1 not included).
    >
    > The "range" can then be increased by:
    >
    > double n2=(rand()+n1)/(RAND_MAX+1);


    That doesn't seem right. Am I missing something?

    john
     
    John Harrison, Apr 7, 2004
    #10
  11. U¿ytkownik "Alan Lee" <> napisa³ w wiadomo¶ci
    news:c503fk$p4j$...
    > Is there any way to set RAND_MAX in rand()? I am looking for a way to get

    a
    > random number between 1 and 1E9. So far I can't figure out how to do

    that.
    > any suggestions would be greatly appreciated.
    >
    >


    Why don't use boost random?

    Best regards,
    Marcin
     
    Marcin Kalicinski, Apr 7, 2004
    #11
  12. Alan Lee

    Pete Becker Guest

    Alan Lee wrote:
    >
    > For now, I don't care too much for the quality as long as its still a random
    > number generator. I will try to find a higher quality generator once I
    > finish my program but I just would like to test it for now. How do I get a
    > larger number by stringing calls to rand together?
    >


    You've gotten the basic advice on how to combine values from multiple
    calls to rand. Don't let the naysayers influence you: even bad
    implementations of rand are good enough for most applications. If you
    determine (based on measurements, not on rumors) that the implementation
    of rand that you're using isn't good enough for what you're doing, then
    it's time to look at other generators.

    --

    Pete Becker
    Dinkumware, Ltd. (http://www.dinkumware.com)
     
    Pete Becker, Apr 7, 2004
    #12
    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. Kingsley Oteng

    Random Number Generator??

    Kingsley Oteng, Apr 27, 2004, in forum: VHDL
    Replies:
    11
    Views:
    52,243
    rahul.iyer
    Aug 9, 2010
  2. tshad

    Random number/letter generator

    tshad, May 5, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    9,567
    tshad
    May 6, 2005
  3. Wally
    Replies:
    1
    Views:
    2,790
    pvdg42
    Mar 20, 2006
  4. globalrev
    Replies:
    4
    Views:
    772
    Gabriel Genellina
    Apr 20, 2008
  5. VK
    Replies:
    15
    Views:
    1,176
    Dr J R Stockton
    May 2, 2010
Loading...

Share This Page