Random numbers for c++

Discussion in 'Mac Programming' started by Serac, Jan 28, 2006.

  1. macrumors newbie

    Joined:
    Jan 26, 2006
    #1
    Taking an example from the program above, I’d like to clarify a question I’ve been having.

    c++
    Code:
    #include <iostream> 
    #include <ctime> 
    #include <cstdlib> 
    using namespace std; 
    
    int main() 
    { 
    srand((unsigned)time(0)); 
    int rn; 
    int lowest=1, highest=100; 
    int range=(highest-lowest)+1; 
    for(int index=0; index<20; index++){ 
    rn=lowest+int(range*rand()/(RAND_MAX + 1.0)); 
    } 
    if (rn<50)
    	{
            cout<<”A random number is under 50”;
            cout<<"("<<rn<<")";
            }
    

    So, with this program, it should supply a random number always different when run, right? Only 1, so I’d have to copy lines 8-14 again if I wanted a different random number?

    Btw, I’m trying to get a “random” number from 1-100.

    I’d appreciate any help. :)
     
  2. macrumors 68030

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
    #2
    srandomdev();
    int rand = (random() * 10000) % 100;

    This is just off the top of my head, but iirc it works fine. random() is better than rand().
     
  3. macrumors 603

    gekko513

    Joined:
    Oct 16, 2003
    #3

    srand(time(0));
    int a = rand() % 100 + 1;

    This gives you a random number between 1 and 100 using srand and rand. Or you can probably use srandomdev as Catfish suggests, but I'm not familiar with that one.

    Catfish, why do you multiply by 10000? If random() returns a long or int, that would make your expression evaluate to 0 every time, wouldn't it?
     
  4. macrumors 68030

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
    #4
    hmm, somehow I'd remembered random() as returning a double between 0 and 1. Like I said, that was just off the top of my head.
     
  5. macrumors 6502

    Joined:
    Nov 30, 2005
    #5
    The general advice when using rand() is that if you can't use all the bits, you should use the higher ones in preference to the lower ones. That's because many "random number" algorithms make a good job of covering the range described by RAND_MAX but a bad job of making every bit truly random, as a result of trying to maximise randomness without spending relatively large processing costs and the choice of random number algorithm is up to whoever implements rand().

    If you could manage it without overflow, ((rand() * range) / RAND_MAX) would therefore always be better than rand()%range. On the Microsoft platform RAND_MAX is only (2^15)-1 or (2^16)-1 so you sometimes see that where people haven't really thought about what they're doing. On OS X RAND_MAX is 0x7fffffff - i.e. the highest positive number that can be stored in an signed int. So you do have to think about range.

    If you're not that bothered about the degree of randomness then %range will probably do. If you can afford the costs, casting to floats and doing a multiply/divide op will supply the best variation. If you can write specifically for your range you can do something like:

    ((rand() >> 8)*range) / (RAND_MAX >> 8)

    (for ranges up to 256, obviously). So for a random number in the inclusive range 0 to 100 you might try:

    ((rand() >> 7)*101) / (RAND_MAX >> 7)
     
  6. macrumors 6502a

    Fender2112

    Joined:
    Aug 11, 2002
    Location:
    Charlotte, NC
    #6
    Here's a little program the asks you to guess a number between 1 and 100. I think the concepts you want can be derived from this simple program.

    #include <iostream>

    using namespace std;

    int main()

    {
    system("clear");

    srandom(time(NULL)); // seed random number generator from internal clock
    int fave;
    fave = (random() % 100) + 1; // use random seed to generate number
    int n;

    //cout << fave << endl;

    cout << "Guess a number from 1 to 100: ";
    do
    {
    cin >> n;
    if (n < fave)
    cout << "Too low. Guess again: ";
    else if (n > fave)
    cout << "Too high. Guess again: ";
    else
    cout << fave << "\a\a\a ***** You must be psycic! ****" << endl;
    } while (n != fave);

    return 0;
    }
     
  7. macrumors 6502a

    Fender2112

    Joined:
    Aug 11, 2002
    Location:
    Charlotte, NC
  8. macrumors 6502a

    Fender2112

    Joined:
    Aug 11, 2002
    Location:
    Charlotte, NC
    #8
    You can write a function and call it each time you need a random number

    void GetRandomNumber() // function prototype

    void GetRandomNumber() // function definition
    {
    srandom(time(NULL));​
    return (random() % 100)+1;​
    }

    x = GetRandomNumber(); // call to function
     
  9. macrumors 6502a

    Joined:
    Jun 17, 2003
    #9
    Since you are reseeding the random number generator each time you invoke this method then the generated numbers can be predicted. (Also, the function needs to be declared as returning an integer.)

    What you really want to do in this case is:

    Code:
    int getRandomNumber();
    
    short randomSeeded = 0;
    
    main() {
      printf("   %d\n", getRandomNumber());
      printf("   %d\n", getRandomNumber());
      printf("   %d\n", getRandomNumber());
    }
    
    int getRandomNumber() {
        if(randomSeeded == 0) {
           srandom(time(NULL));
           randomSeeded = 1;
        }
    
        return (random()%100) + 1;
    }
    
     

Share This Page