Thread: the rand() function

  1. #1
    Unregistered
    Guest

    Talking the rand() function

    ok y is it that when i try do do something like this:


    #include <iostream>
    #include <string>

    using namespace std;

    int rand();

    int main ()
    {
    cout << rand();

    return 0;
    }

    it outputs the same number every time? i dont get it. i thought it was supposed to be random.

  2. #2
    Registered User
    Join Date
    Sep 2001
    Location
    Fiji
    Posts
    212
    the rand function is psuedo-random.

    Which means it appears to be random but actually isn't, if you do it a few more times in your program and run two or more times the same numbers are printed.

    To get a more random generation of numbers, you have to seed the rand function with a number example

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    
    int main() {
        srand(time(NULL));  // this line seed's the rand function
        for (int i=0; i <10 ; i++){
            cout << rand();
        }
        return 0;
    }

    hope this helps


    PS the rand function without seeding it is normally used to test side effects of programs and there correctness.

  3. #3
    Unregistered
    Guest
    great. i get it now. thanks buddy.

  4. #4
    Registered User Mario's Avatar
    Join Date
    May 2002
    Posts
    317
    Originally posted by kwigibo
    PS the rand function without seeding it is normally used to test side effects of programs and there correctness.
    Could you elaborate a bit more on this please?
    Regards,
    Mario Figueiredo
    Using Borland C++ Builder 5

    Read the Tao of Programming
    This advise was brought to you by the Comitee for a Service Packless World

  5. #5
    Registered User
    Join Date
    Sep 2001
    Location
    Fiji
    Posts
    212
    Well, you see large programs that use "random" data use the psuedo-random numbers to test that the algorithm works. Because they know what numbers to expect. And can test the outcome. So when they know the algorithm works they can use "real random" numbers in the program. And trust it's results. Hope that helps.

    kwigibo

  6. #6
    Registered User Mario's Avatar
    Join Date
    May 2002
    Posts
    317
    Ok. got it
    Regards,
    Mario Figueiredo
    Using Borland C++ Builder 5

    Read the Tao of Programming
    This advise was brought to you by the Comitee for a Service Packless World

  7. #7
    that's a good way to do it

  8. #8
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537

    Re: the rand() function

    Originally posted by Unregistered
    ok y is it that when i try do do something like this:


    #include <iostream>
    #include <string>

    using namespace std;

    int rand();

    int main ()
    {
    cout << rand();

    return 0;
    }

    it outputs the same number every time? i dont get it. i thought it was supposed to be random.

    There is no need to declare rand before main. In fact I wouldnt have expected it to compile like that, shows what I know

  9. #9
    it's a waste of filesize, because you are basically prototyping a prototype.

Popular pages Recent additions subscribe to a feed