Random # woes

This is a discussion on Random # woes within the C++ Programming forums, part of the General Programming Boards category; can any one tell me why this random # generator only returns 42 every time #include <stdAfx.h> #include <iostream.h> #include ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    57

    Random # woes

    can any one tell me why this random # generator only returns 42 every time

    #include <stdAfx.h>
    #include <iostream.h>
    #include <stdlib.h> //for rand
    //for printf
    #include <time.h>

    int rand_mid(int low,int high);

    int main(int argc, char* argv[])
    {

    cout << endl;
    cout << "you must role the dice here is the result of your role: ";
    cout << rand_mid(1,100);


    cout << endl;
    return 0;
    }
    int rand_mid(int low, int high)
    {
    return low+rand()%(high-low+1);
    }


    I got this code out of the FAQ section
    =@-OmegatronO-@=

  2. #2
    uvacow
    Guest

    seeding, just like your tulips

    hm. haven't done random numbers in a while, but you need to seed it with the current time.

    What i figured was rand just takes the seed you give it, does a whole bunch of operations and gives you a result. However, you seeded it with a constant, and therefore it gives the same result. MSDN help files give u the correct code and they tell you bout seeding. But yeh, u need to use time somehow.

    cow

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    591
    You need to seed rand with the srand() function

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    57
    The problem is that if i seed it with the time and that function gets used over and over again its actually not random because each time you do it you can tell what the next number is gonna be

    its because its counting too slow maybe i need to have the number cycle in faster time increments
    =@-OmegatronO-@=

  5. #5
    Registered User
    Join Date
    Feb 2002
    Posts
    591
    Thats usally happens when you seed more then once. srand should only be used once. After the first time rand uses last result to "reseed" itself

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    57

    Unhappy

    Yaa does anyone have any suggestions for a random # generator that can handle multiple executions over a reletivly short period of time
    =@-OmegatronO-@=

  7. #7
    Registered User
    Join Date
    Feb 2002
    Posts
    591
    There shouldn't be a need for anything else then srand(). just put it in your main() and it will seed all your rand() in all functions

  8. #8
    Registered User
    Join Date
    Feb 2002
    Posts
    591
    Code:
    #include <stdAfx.h> 
    #include <iostream.h> 
    #include <stdlib.h> //for rand 
    //for printf 
    #include <time.h> 
    
    int rand_mid(int low,int high); 
    
    int main(int argc, char* argv[]) 
    { 
       srand(time(NULL));
       cout << endl; 
       cout << "you must role the dice here is the result of your role: "; 
       cout << rand_mid(1,100); 
       cout << endl; 
       return 0; 
    } 
    
    int rand_mid(int low, int high) 
    { 
       return low+rand()%(high-low+1); 
    }

  9. #9
    Registered User
    Join Date
    Aug 2001
    Posts
    57
    Oh well ill just have to live with that
    =@-OmegatronO-@=

  10. #10
    Registered User
    Join Date
    Feb 2002
    Posts
    591
    Nothing stoping you from doing this.
    Code:
    #include <stdAfx.h> 
    #include <iostream.h> 
    #include <stdlib.h> //for rand 
    //for printf 
    #include <time.h> 
    
    int rand_mid(int low,int high); 
    
    int main(int argc, char* argv[]) 
    { 
       srand(time(NULL));
       cout << endl; 
       cout << "you must role the dice here is the result of your role: "; 
       for(int i = 0; i < 100; i++)
       {
          cout << rand_mid(1,100); 
          cout << endl;
       } 
       return 0; 
    } 
    
    int rand_mid(int low, int high) 
    { 
       return low+rand()%(high-low+1); 
    }

  11. #11
    uvacow
    Guest

    if you srand (seed)

    if you use srand just once, it will be about as random as u get. Seed it once then just rand the rest.

    cow

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. random to int?
    By psyadam in forum C# Programming
    Replies: 7
    Last Post: 07-22-2008, 08:09 PM
  2. Lesson #3 - Math
    By oval in forum C# Programming
    Replies: 2
    Last Post: 04-27-2006, 08:16 AM
  3. Another brain block... Random Numbers
    By DanFraser in forum C# Programming
    Replies: 2
    Last Post: 01-23-2005, 04:51 PM
  4. How do I restart a random number sequence.
    By jeffski in forum C Programming
    Replies: 6
    Last Post: 05-29-2003, 02:40 PM
  5. Best way to generate a random double?
    By The V. in forum C Programming
    Replies: 3
    Last Post: 10-16-2001, 04:11 PM

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