random is not random enough?

Discussion in 'C++' started by JNY, Dec 29, 2004.

  1. JNY

    JNY Guest

    I am using random to generate random numbers, thus:

    int x,y;

    for (y = 0;y < 5;y++)
    {
    x = random(50);
    cout << x;
    }

    When I run this program, 5 random numbers are produced. However, if I
    stop the program and re-run it, the same sequence is produced. Am I
    forgetting to do something? Is there another random number generator
    which I could try?
     
    JNY, Dec 29, 2004
    #1
    1. Advertising

  2. JNY wrote:
    > I am using random to generate random numbers, thus:
    >
    > int x,y;
    >
    > for (y = 0;y < 5;y++)
    > {
    > x = random(50);
    > cout << x;
    > }
    >
    > When I run this program, 5 random numbers are produced. However, if I
    > stop the program and re-run it, the same sequence is produced. Am I
    > forgetting to do something? Is there another random number generator
    > which I could try?


    You are forgetting to seed the generator. RTFM on 'srand' function.

    V
     
    Victor Bazarov, Dec 29, 2004
    #2
    1. Advertising

  3. JNY

    Ramspite Guest

    JNY wrote:

    > I am using random to generate random numbers, thus:
    >
    > int x,y;
    >
    > for (y = 0;y < 5;y++)
    > {
    > x = random(50);
    > cout << x;
    > }
    >
    > When I run this program, 5 random numbers are produced. However, if I
    > stop the program and re-run it, the same sequence is produced. Am I
    > forgetting to do something? Is there another random number generator
    > which I could try?


    Your missing the srand function. check you docs or google it for details.
     
    Ramspite, Dec 29, 2004
    #3
  4. JNY

    dandelion Guest

    "JNY" <> wrote in message
    news:...
    > I am using random to generate random numbers, thus:
    >
    > int x,y;
    >
    > for (y = 0;y < 5;y++)
    > {
    > x = random(50);
    > cout << x;
    > }
    >
    > When I run this program, 5 random numbers are produced. However, if I
    > stop the program and re-run it, the same sequence is produced. Am I
    > forgetting to do something?


    Yes. Seed the random generator with some number (the number of seconds since
    EPOCH, for instance).

    See http://www.manpage.org/cgi-bin/man/man2html?query=srand

    > Is there another random number generator which I could try?


    Many.
     
    dandelion, Dec 29, 2004
    #4
  5. JNY

    KPB Guest

    Victor Bazarov wrote:
    > JNY wrote:
    >
    >> I am using random to generate random numbers, thus:
    >>
    >> int x,y;
    >>
    >> for (y = 0;y < 5;y++)
    >> {
    >> x = random(50);
    >> cout << x;
    >> }
    >>
    >> When I run this program, 5 random numbers are produced. However, if I
    >> stop the program and re-run it, the same sequence is produced. Am I
    >> forgetting to do something? Is there another random number generator
    >> which I could try?

    >
    >
    > You are forgetting to seed the generator. RTFM on 'srand' function.
    >
    > V



    I don't mean to split hairs but isn't the function call supposed to be
    rand() not random()?

    What am I missing? I know srand()/rand() is part of (<cstdlib>,
    <cmath>... I forget which) but don't know about random().

    KPB
     
    KPB, Dec 29, 2004
    #5
  6. JNY

    dandelion Guest

    "KPB" <> wrote in message news:VXzAd.24695$...

    <snip>

    > I don't mean to split hairs but isn't the function call supposed to be
    > rand() not random()?


    Ummm...

    Right.

    > What am I missing?


    From a quick glance in the standard, it appears that you
    aren't missing anything.

    I know srand()/rand() is part of (<cstdlib>,
    > <cmath>... I forget which) but don't know about random().


    <cstdlib> and <cmath> are C++ header files.

    Phew... I'm not the only one who makes mistakes...
     
    dandelion, Dec 29, 2004
    #6
  7. JNY

    Guest

    JNY wrote:
    > I am using random to generate random numbers, thus:
    >
    > int x,y;
    >
    > for (y = 0;y < 5;y++)
    > {
    > x = random(50);
    > cout << x;
    > }
    >
    > When I run this program, 5 random numbers are produced. However, if

    I
    > stop the program and re-run it, the same sequence is produced. Am I
    > forgetting to do something? Is there another random number generator
    > which I could try?


    Random is not a basic C++ function. The ones you'd want to use are
    srand and rand. because of this I'm going to assume a few things.
    That is that under the hood this random calls srand once and then calls
    rand after that.
    If that is the case then your random does exactly what it is supposed
    to do.
    The rand function is a pseudo random generator. It starts from a seed
    number (50 in this case) and based on that value returns you the next
    value in a sequence (and uses the returned value to calculate the next
    value of the sequence).
     
    , Dec 29, 2004
    #7
  8. JNY

    KPB Guest

    dandelion wrote:

    >
    > I know srand()/rand() is part of (<cstdlib>,
    >
    >><cmath>... I forget which) but don't know about random().

    >
    >
    > <cstdlib> and <cmath> are C++ header files.


    Yes. That I know. In this NG, I considered that info implicit.

    If I had said, it's in <random.h>, well, that's not a header file that's
    part of the C++ standard library so it wouldn't have been applicable here.

    > Phew... I'm not the only one who makes mistakes...


    I make my share, believe me but I don't think I made one here.
     
    KPB, Dec 29, 2004
    #8
  9. JNY

    Guest


    > Random is not a basic C++ function.


    dangit that should have been
    Random is not a basic C++ function IIRC.

    (always do cya :) )
     
    , Dec 29, 2004
    #9
  10. KPB wrote:
    > dandelion wrote:
    >
    >>
    >> I know srand()/rand() is part of (<cstdlib>,
    >>
    >>> <cmath>... I forget which) but don't know about random().

    >>
    >>
    >>
    >> <cstdlib> and <cmath> are C++ header files.

    >
    >
    > Yes. That I know. In this NG, I considered that info implicit.


    I guess you didn't notice that the thread is cross-posted...

    > [...]
     
    Victor Bazarov, Dec 29, 2004
    #10
  11. JNY

    KPB Guest

    Victor Bazarov wrote:
    > KPB wrote:
    >
    >> dandelion wrote:
    >>
    >>>
    >>> I know srand()/rand() is part of (<cstdlib>,
    >>>
    >>>> <cmath>... I forget which) but don't know about random().
    >>>
    >>>
    >>>
    >>>
    >>> <cstdlib> and <cmath> are C++ header files.

    >>
    >>
    >>
    >> Yes. That I know. In this NG, I considered that info implicit.

    >
    >
    > I guess you didn't notice that the thread is cross-posted...
    >
    >> [...]


    Oops. Nope. That explains a lot.

    Sorry to you C guys. I didn't mean to cross-post. This'll be my last one.
     
    KPB, Dec 29, 2004
    #11
  12. JNY

    Michael Mair Guest

    KPB wrote:
    > dandelion wrote:
    >
    >> KPB wrote
    >>> I know srand()/rand() is part of (<cstdlib>,
    >>> <cmath>... I forget which) but don't know about random().

    >>
    >> <cstdlib> and <cmath> are C++ header files.

    >
    > Yes. That I know. In this NG, I considered that info implicit.
    >
    > If I had said, it's in <random.h>, well, that's not a header file that's
    > part of the C++ standard library so it wouldn't have been applicable here.


    The problem is, in _these_ NGs (c.l.c, c.l.c++), one half of the
    participants does not (officially) know <cstdlib> and <cmath>...


    Cheers
    Michael
    --
    E-Mail: Mine is a gmx dot de address.
     
    Michael Mair, Dec 29, 2004
    #12
  13. JNY

    KPB Guest

    Michael Mair wrote:
    > KPB wrote:
    >
    >> dandelion wrote:
    >>
    >>> KPB wrote
    >>>
    >>>> I know srand()/rand() is part of (<cstdlib>,
    >>>> <cmath>... I forget which) but don't know about random().
    >>>
    >>>
    >>> <cstdlib> and <cmath> are C++ header files.

    >>
    >>
    >> Yes. That I know. In this NG, I considered that info implicit.
    >>
    >> If I had said, it's in <random.h>, well, that's not a header file
    >> that's part of the C++ standard library so it wouldn't have been
    >> applicable here.

    >
    >
    > The problem is, in _these_ NGs (c.l.c, c.l.c++), one half of the
    > participants does not (officially) know <cstdlib> and <cmath>...
    >
    >
    > Cheers
    > Michael



    Yes because I was an idiot and accidently cross-posted to the C group.
    It wasn't my intention and it won't happen again.
     
    KPB, Dec 29, 2004
    #13
  14. JNY

    infobahn Guest

    KPB wrote:
    > dandelion wrote:
    >
    >>
    >> I know srand()/rand() is part of (<cstdlib>,
    >>
    >>> <cmath>... I forget which) but don't know about random().

    >>
    >>
    >>
    >> <cstdlib> and <cmath> are C++ header files.

    >
    >
    > Yes. That I know. In this NG, I considered that info implicit.


    But "this NG" doesn't mean a lot when the thread is cross-posted.

    In comp.lang.c, <cstdlib> and <cmath> are off-topic. In comp.lang.c++,
    they are topical. The cross-posting is probably inappropriate.
     
    infobahn, Dec 29, 2004
    #14
  15. JNY

    KPB Guest

    infobahn wrote:
    > KPB wrote:
    >
    >> dandelion wrote:
    >>
    >>>
    >>> I know srand()/rand() is part of (<cstdlib>,
    >>>
    >>>> <cmath>... I forget which) but don't know about random().
    >>>
    >>>
    >>>
    >>>
    >>> <cstdlib> and <cmath> are C++ header files.

    >>
    >>
    >>
    >> Yes. That I know. In this NG, I considered that info implicit.

    >
    >
    > But "this NG" doesn't mean a lot when the thread is cross-posted.
    >
    > In comp.lang.c, <cstdlib> and <cmath> are off-topic. In comp.lang.c++,
    > they are topical. The cross-posting is probably inappropriate.
    >


    Am I going to have to apologize to each and every one of you or will you
    read on to the next posts in this thread first?
     
    KPB, Dec 29, 2004
    #15
  16. JNY

    Michael Mair Guest

    KPB wrote:
    > infobahn wrote:

    [snip: KPB missed the fact that the thread is crossposted to both
    c.l.c and c.l.c++]

    >> But "this NG" doesn't mean a lot when the thread is cross-posted.
    >>
    >> In comp.lang.c, <cstdlib> and <cmath> are off-topic. In comp.lang.c++,
    >> they are topical. The cross-posting is probably inappropriate.

    >
    > Am I going to have to apologize to each and every one of you or will you
    > read on to the next posts in this thread first?


    Stop apologizing :)
    The problem is that the usenet is asynchronous, so some of us
    will not see the answers of others for some minutes, hours or
    days after they have been sent.


    Cheers
    Michael
    --
    E-Mail: Mine is a gmx dot de address.
     
    Michael Mair, Dec 29, 2004
    #16
  17. JNY

    KPB Guest

    Michael Mair wrote:

    > Stop apologizing :)
    > The problem is that the usenet is asynchronous, so some of us
    > will not see the answers of others for some minutes, hours or
    > days after they have been sent.


    Ok. :)
     
    KPB, Dec 29, 2004
    #17
  18. JNY wrote:

    > I am using random to generate random numbers, thus:
    >
    > int x,y;
    >
    > for (y = 0;y < 5;y++)
    > {
    > x = random(50);
    > cout << x;
    > }
    >
    > When I run this program, 5 random numbers are produced. However, if I
    > stop the program and re-run it, the same sequence is produced. Am I
    > forgetting to do something? Is there another random number generator
    > which I could try?


    See Ramdom1 to Ramdom14 in
    http://remus.rutgers.edu/~rhoads/Code/code.html

    You can use the time (perhaps to the millisecond) as a seed.

    gtoomey
     
    Gregory Toomey, Dec 29, 2004
    #18
  19. JNY wrote:
    > I am using random to generate random numbers, thus:
    >
    > int x,y;
    >
    > for (y = 0;y < 5;y++)
    > {
    > x = random(50);
    > cout << x;
    > }
    >
    > When I run this program, 5 random numbers are produced. However, if I
    > stop the program and re-run it, the same sequence is produced. Am I
    > forgetting to do something? Is there another random number generator
    > which I could try?


    1) 'random' is not the name of the standard library function for
    generating pseudo-random numbers. That function is named 'rand'

    2) If rand is used without first seeding with the srand function, it
    will provide the same sequence on every run. This is a GoodThing(tm),
    since it allows testing and debugging on the same sequence of values.

    3) All this and more is covered in the FAQ
    <http://www.eskimo.com/~scs/C-faq/top.html>, in particular see the
    sections on random numbers, including
    13.15 I need a random number generator.
    13.16 How can I get random integers in a certain range?
    13.17 Each time I run my program, I get the same sequence of numbers
    back from rand().
    13.18 I need a random true/false value, so I'm just taking rand() % 2,
    but it's alternating 0, 1, 0, 1, 0...
    13.20 How can I generate random numbers with a normal or Gaussian
    distribution?

    You will note that 13.17 is just the question that you asked. Always
    check the FAQ before posting. Steve Summit has put in a lot of work
    making sure that reliable answers are available to common questions.
    This ensures that you don't have to put up with incorrect answers from
    the clueless and that you don't suffer the anger of people who see the
    same, already answered questions appearing here every September, no
    matter what month September is in.
     
    Martin Ambuhl, Dec 29, 2004
    #19
  20. On Wed, 29 Dec 2004 10:17:12 -0500, KPB <> wrote:

    >Victor Bazarov wrote:
    >
    >I don't mean to split hairs but isn't the function call supposed to be
    >rand() not random()?


    random() is a non-standard function (peculiar to DOS implementations, if
    not others) that returns a random number between 0 and num-1:

    int random(int num)

    >What am I missing? I know srand()/rand() is part of (<cstdlib>,
    ><cmath>... I forget which) but don't know about random().


    #include <stdlib.h>

    As this post was cross-posted to comp.lang.c., let me add the obligatory
    comment that <cstdlib> and <cmath> are not standard C header files, but
    are in fact C++ headers.

    The OP's code suggests C++, but since he cross-posted to c.l.c as well,
    thought I'd mention it....

    --
    Robert B. Clark (email ROT13'ed)
    Visit ClarkWehyr Enterprises On-Line at http://www.3clarks.com/ClarkWehyr/
     
    Robert B. Clark, Dec 29, 2004
    #20
    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. Peter
    Replies:
    0
    Views:
    2,340
    Peter
    Jun 27, 2003
  2. jobs
    Replies:
    0
    Views:
    1,528
  3. JNY

    random is not random enough?

    JNY, Dec 29, 2004, in forum: C Programming
    Replies:
    27
    Views:
    1,142
    Lawrence Kirby
    Dec 30, 2004
  4. globalrev
    Replies:
    4
    Views:
    810
    Gabriel Genellina
    Apr 20, 2008
  5. VK
    Replies:
    15
    Views:
    1,321
    Dr J R Stockton
    May 2, 2010
Loading...

Share This Page