how

This is a discussion on how within the C++ Programming forums, part of the General Programming Boards category; how would u get different numbers in the 2 loops? code: #include <stdlib.h> #include <time.h> #include <iostream.h> int funk1() { ...

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    479

    how

    how would u get different numbers in the 2 loops?
    code:
    #include <stdlib.h>
    #include <time.h>
    #include <iostream.h>

    int funk1()
    {

    srand((unsigned)time(NULL));

    int d=rand()%7;
    cout<<d<<flush;
    return 0;

    }
    int funk2()
    {

    srand((unsigned)time(NULL));

    int d=rand()%7;

    cout<<d<<flush;
    return 0;
    }
    main()
    {
    funk1();
    cout<<"--";
    funk2();
    return 0;
    }

  2. #2
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    What?
    Blue

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    like this....
    Code:
    #include <stdlib.h> 
    #include <time.h> 
    #include <iostream.h> 
    
    int funk1() 
    { 
    int d=rand()%7; 
    cout<<d<<flush; 
    return 0; 
    } 
    
    int funk2() 
    { 
    int d=rand()%7; 
    cout<<d<<flush; 
    return 0; 
    } 
    
    int main() 
    {
    srand((unsigned)time(NULL));  
    funk1(); 
    cout<<"--"; 
    funk2(); 
    return 0; 
    }
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  4. #4
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    You don't want to call srand more than once (especially if you're using the time as the argument).

    rand() will be seeded with the value entered into srand(), this is the value that it will use to begin producing pseudo random numbers. If you use the time to seed your random number generator, and then re-seed before time has advanced, you're restarting the number series produced by rand() with the same seed and will therefore get the same result.

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    thnx for the help m8'ts!

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21