My random number is only random for the first run???

Discussion in 'C++' started by xeys_00, Apr 9, 2005.

  1. xeys_00

    xeys_00 Guest

    #include<iostream>
    #include<cstdlib>



    //This program will be a number guessing game

    int main()
    {
    //variables for the game
    int number_of_tries=0;//how many times did it take to get it right?
    int current_guess=0;//the number being currently tried
    int upper_limit=100;
    int lower_limit=1;
    int correct_number=rand()%100;//the correct number will be from no
    higher than 100
    while (correct_number != current_guess)
    {
    //user interaction
    std::cout << "What is your guess? The correct number will be no higher
    than 100, or lower than 1.\n";
    std::cin >> current_guess;//user inputs his guess
    if (current_guess > upper_limit || current_guess < lower_limit)
    {
    std::cout << "What, you can't read directions? The number has to be
    no higher than 100, or lower than 1!!! You're Fired!!!!!\n";
    std::abort();//kicks user out if input is bad
    }
    else if (current_guess < correct_number)
    {
    std::cout << "Your guess is too low. Try again\n";
    number_of_tries++;
    }
    else if (current_guess > correct_number)
    {
    std::cout << "Your guess is too high. Try again\n";
    number_of_tries++;
    }
    else if (current_guess == correct_number)
    {
    std::cout << "Good job! You guessed the right number!!!\n";
    std::cout << "It only took you "<< number_of_tries << " tries!\n";
    break;
    }
    }

    return 0;


    }


    All is good for the first run. Then the wierdest thing happens... The
    "random" number is always the same forever afterwards.
    xeys_00, Apr 9, 2005
    #1
    1. Advertising

  2. xeys_00

    CrayzeeWulf Guest

    xeys_00 wrote:
    > All is good for the first run. Then the wierdest thing happens... The
    > "random" number is always the same forever afterwards.

    The function rand() generates pseudo-random numbers. You need to use srand()
    to seed it with a different seed every time you run the program in order to
    generate different sequences of random number. See the following:

    http://cplus.about.com/od/cprogrammingtips/l/aa041403b.htm

    Later,
    --
    CrayzeeWulf
    CrayzeeWulf, Apr 9, 2005
    #2
    1. Advertising

  3. xeys_00 wrote:
    > int correct_number=rand()%100;//the correct number will be from no
    > higher than 100


    You're not initializing the random seed. Do something like

    srand(time(NULL));

    at the very start and you'll have more "random" random numbers (although
    still remaining highly deterministic).

    Although in this very application not really necessary, you should
    follow the advice from rand(3) how to avoid using lower-order bits in
    your random numbers.

    Greetings,
    Johannes

    --
    PLEASE verify my signature. Some forging troll is claiming to be me.
    My GPG key id is 0xCC727E2E (dated 2004-11-03). You can get it from
    wwwkeys.pgp.net or random.sks.keyserver.penguin.de.
    Also: Messages from "Comcast Online" are ALWAYS forged.

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.1 (GNU/Linux)
    Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

    iD8DBQFCWGe5CseFG8xyfi4RAvMxAJsE/6CeRJdHiBXgdOGyPmmhHX5JUgCfTu1z
    wH+EA75o+BSr6p9RTKQVwps=
    =xX7J
    -----END PGP SIGNATURE-----
    Johannes Bauer, Apr 10, 2005
    #3
  4. xeys_00

    wwwolf Guest

    Johannes Bauer wrote:
    > Do something like
    >
    > srand(time(NULL));
    >


    Is there any difference at all between srand(time(NULL)); and srand(time(0));?


    > you should
    > follow the advice from rand(3) how to avoid using lower-order bits in
    > your random numbers.


    Where is this advice, and how do I get it?

    Thanx for your help,

    wwwolf
    wwwolf, Apr 10, 2005
    #4
  5. wwwolf wrote:
    > Johannes Bauer wrote:
    >
    >>Do something like
    >>
    >>srand(time(NULL));
    >>

    > Is there any difference at all between srand(time(NULL)); and srand(time(0));?


    Well, yes. 0 is the number zero. NULL is the pointer zero, usually
    defined as "(void*)0". The time(2) call is system specific; however on
    my box, a Linux system, it demands a "time_t*" as an argument. It
    requires a pointer. NULL is a pointer, 0 is not.

    So when you want to go safe, use NULL, although "0" might work when your
    compiler doesn't take type-safety too seriously (which _you_ should).

    >>you should
    >>follow the advice from rand(3) how to avoid using lower-order bits in
    >>your random numbers.

    >
    > Where is this advice, and how do I get it?


    Try typing "man 3 rand" - when you've manual pages installed it comes
    up. When you don't, here it is:

    ------------
    In Numerical Recipes in C: The Art of Scientific Computing (William
    H. Press, Brian P. Flannery, Saul A. Teukolsky, William T. Vetterling;
    New York: Cambridge University Press, 1992 (2nd ed., p. 277)), the
    following comments are made:

    "If you want to generate a random integer between 1 and 10, you should
    always do it by using high-order bits, as in

    j=1+(int) (10.0*rand()/(RAND_MAX+1.0));

    and never by anything resembling

    j=1+(rand() % 10);

    (which uses lower-order bits)."
    ------------

    Greetings,
    Johannes

    --
    PLEASE verify my signature. Some forging troll is claiming to be me.
    My GPG key id is 0xCC727E2E (dated 2004-11-03). You can get it from
    wwwkeys.pgp.net or random.sks.keyserver.penguin.de.
    Also: Messages from "Comcast Online" are ALWAYS forged.

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.1 (GNU/Linux)
    Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

    iD8DBQFCWJKiCseFG8xyfi4RAmMyAJ9Hg+plvUgD+h3WmnWoEFJBzq/KmwCgkA4P
    ULAG03W9y3zzeO4HlIq4Hg0=
    =Nskj
    -----END PGP SIGNATURE-----
    Johannes Bauer, Apr 10, 2005
    #5
  6. xeys_00

    Evan Guest

    >> you should
    >> follow the advice from rand(3) how to avoid using lower-order bits

    in
    >> your random numbers.

    >Where is this advice, and how do I get it?


    I'm going to go out on a limb and guess that since you don't know about
    the rand(3) style syntax, you're not on a *nix platform and Johannes's
    instructions to type "man 3 rand" will be met with an error. In any
    case, his assumption that you have man pages is quite unreasonable.

    See
    http://www.freebsd.org/cgi/man.cgi?...ath=FreeBSD 5.3-RELEASE and Ports&format=html
    Evan, Apr 10, 2005
    #6
  7. xeys_00

    wwwolf Guest

    Evan wrote:

    >>> you should
    >>> follow the advice from rand(3) how to avoid using lower-order bits

    > in
    >>> your random numbers.

    >>Where is this advice, and how do I get it?

    >
    > I'm going to go out on a limb and guess that since you don't know about
    > the rand(3) style syntax, you're not on a *nix platform and Johannes's
    > instructions to type "man 3 rand" will be met with an error. In any
    > case, his assumption that you have man pages is quite unreasonable.
    >
    > See
    > http://www.freebsd.org/cgi

    man.cgi?query=rand&apropos=0&sektion=3&manpath=FreeBSD+5.3-RELEASE+an
    +Ports&format=html

    OK, I must have had a brain fart. I wasn't thinking man at the time. rand(3)
    looked too much like function() and I got a little confused.

    Thanx VERY MUCH for that link. I think I will be using it often!!!
    wwwolf, Apr 10, 2005
    #7
  8. xeys_00

    wwwolf Guest

    Johannes Bauer wrote:
    <snip>
    > Try typing "man 3 rand" - when you've manual pages installed it comes
    > up.


    Thanx, I wasn't aware that the man pages could explain functions to me. This has
    opened up a whole new resource for me!
    wwwolf, Apr 10, 2005
    #8
  9. xeys_00

    xeys_00 Guest

    Well, thanks much. I will definitely play around with this and probably
    add stuff to the prog. But I want to get the basic functionality
    working first.

    Xeys
    xeys_00, Apr 10, 2005
    #9
  10. "Johannes Bauer" <> skrev i en meddelelse
    news:...

    wwwolf wrote:
    > Johannes Bauer wrote:
    >
    >>Do something like
    >>
    >>srand(time(NULL));
    >>

    > Is there any difference at all between srand(time(NULL)); and
    > srand(time(0));?


    Johannes Baquer also wrote:
    "Well, yes. 0 is the number zero. NULL is the pointer zero, usually
    defined as "(void*)0". The time(2) call is system specific; however on
    my box, a Linux system, it demands a "time_t*" as an argument. It
    requires a pointer. NULL is a pointer, 0 is not.

    So when you want to go safe, use NULL, although "0" might work when your
    compiler doesn't take type-safety too seriously (which _you_ should)."

    That is simply not true. First NULL simply is not defined as (void*)0 as
    this would simply not work. In C++ there is no implicit conversion from
    void*.
    Secondly, 0 is the (only possible) representation for a null-pointer and is
    both portable and type-safe. In fact you will find many (Stroustrup is one
    of these) who recommend you use 0, not NULL.

    /Peter
    Peter Koch Larsen, Apr 10, 2005
    #10
  11. Peter Koch Larsen wrote:
    > "Johannes Bauer" <> skrev i en meddelelse
    > news:...
    >
    > wwwolf wrote:
    >
    >>Johannes Bauer wrote:
    >>
    >>
    >>>Do something like
    >>>
    >>>srand(time(NULL));
    >>>

    >>
    >>Is there any difference at all between srand(time(NULL)); and
    >>srand(time(0));?

    >
    >
    > Johannes Baquer also wrote:
    > "Well, yes. 0 is the number zero. NULL is the pointer zero, usually
    > defined as "(void*)0". The time(2) call is system specific; however on
    > my box, a Linux system, it demands a "time_t*" as an argument. It
    > requires a pointer. NULL is a pointer, 0 is not.
    >
    > So when you want to go safe, use NULL, although "0" might work when your
    > compiler doesn't take type-safety too seriously (which _you_ should)."
    >
    > That is simply not true. First NULL simply is not defined as (void*)0 as
    > this would simply not work. In C++ there is no implicit conversion from
    > void*.


    You're right, I was thinking in C, not C++.

    > Secondly, 0 is the (only possible) representation for a null-pointer and is
    > both portable and type-safe. In fact you will find many (Stroustrup is one
    > of these) who recommend you use 0, not NULL.


    Why is that? Doesn't that defy all object-oriented concepts? I mean,
    using pointers alone isn't very OO, but using integer numbers as
    pointers is, well, awkward.

    What reason would there be to use 0 in favor of NULL?

    Greetings,
    Johannes

    --
    PLEASE verify my signature. Some forging troll is claiming to be me.
    My GPG key id is 0xCC727E2E (dated 2004-11-03). You can get it from
    wwwkeys.pgp.net or random.sks.keyserver.penguin.de.
    Also: Messages from "Comcast Online" are ALWAYS forged.

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.1 (GNU/Linux)
    Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

    iD8DBQFCWTQCCseFG8xyfi4RAll5AKCcUwTZRpoMoG9JUvr0J6I/Hy8OagCgnasK
    vI63lPWUQeIPvBvrVErQ4sM=
    =aZBO
    -----END PGP SIGNATURE-----
    Johannes Bauer, Apr 10, 2005
    #11
  12. xeys_00

    Ravi Guest

    Johannes Bauer wrote:
    > What reason would there be to use 0 in favor of NULL?


    int main()
    {
    int *p = NULL;
    return 0;
    }

    is a program that will not compile

    int main()
    {
    int *p = 0;
    return 0;
    }

    is a program that will compile. it is not the same as:

    int main()
    {
    int main()
    {
    int x = 0;
    int *p = x;
    return 0;
    }

    because the compiler cannot guarantee x will be 0. There are two
    special things about the value 0:
    1) You can assign a pointer to it with no cast for any pointer type.
    2) A lot of code is written to check for equality to 0 before using the
    pointer. deletion of a pointer that happens to equal to 0 is also
    checked and safe too.

    NULL is just a
    #define NULL 0
    rather idiotic to use especially because its in some random header file.
    Ravi, Apr 10, 2005
    #12
  13. As a corollary to this srand/rand question, a colleague of mine
    has this code:

    // Initialize the random number generator
    uint32_t seed; seed += getpid() * time(NULL);
    srand(seed);
    int n = (seed >> 16) & 0xFFFF;
    for (int i = 0; i < n; ++i) rand();

    When I asked him if calling rand a random number of times would
    generate a more random number, his answer was that it makes the
    number "less predictable but not more random." Not sure I
    understand the subtleness of the answer, but is it true?

    --
    Jonathan Arnold (mailto:)
    The Incredible Brightness of Seeing, a Home Theater weblog
    http://www.anaze.us/HomeTheater
    Jonathan Arnold, Apr 11, 2005
    #13
    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. DKode

    Run method only on first load?

    DKode, Nov 21, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    406
    Chris Jackson
    Nov 21, 2003
  2. globalrev
    Replies:
    4
    Views:
    745
    Gabriel Genellina
    Apr 20, 2008
  3. bio
    Replies:
    6
    Views:
    301
    BartC
    Oct 2, 2010
  4. Replies:
    2
    Views:
    153
    Tad McClellan
    Mar 26, 2005
  5. VK
    Replies:
    15
    Views:
    1,125
    Dr J R Stockton
    May 2, 2010
Loading...

Share This Page