Thread: generating random integers from 0 to 17576999

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    16

    generating random integers from 0 to 17576999

    RAND_MAX for my system is 32767 so rand() cannot generate an integer greater than this. The only method I think would work is
    Code:
    int generateRandomInt() {
    int randomInt=0;
    for (int i=0; i<536; ++i)
    randomInt+=rand();
    return randomInt;
    }
    Is there any other way?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You can do this, but you won't get a uniform distribution (for example, to get 0 out of this function, rand() would have to be 0 536 times in a row). If this is a problem, well, you can learn how to write a random number generator, or see if you can find another one.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    16
    Quote Originally Posted by tabstop View Post
    You can do this, but you won't get a uniform distribution (for example, to get 0 out of this function, rand() would have to be 0 536 times in a row).
    I realized that too. I tried modifying the code to
    Code:
    int generateRandInt() {
    	int randInt=0, numOfLoops=rand()%537;
    	for (int i=0; i<numOfLoops; ++i)
    		randInt+=rand();
    	return randInt;
    }
    and the distribution got a lot better.

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Couldn't you call rand() twice, shift the first one left 16 bits and then OR them together to get a 32 bit random number?
    Code:
    unsigned int randHigh = (rand() << 16);
    unsigned int randNum = (randHigh | rand());
    Whether this produces a very good random number or not, I'm not sure? To make the range smaller, just tweak the left shift a bit and add a % operator...

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I think you might get something more uniform with code like
    Code:
    int multipilier = rand()&#37;537; 
    int randInt = multiplier * 32768 + rand();
    and of course *32768 might be represented as a bit-shift, if you feel like that sort of thing. Whether or not this matters depends on what you're doing with this thing.

    Edit: My point being that you've swung the pendulum the other way: 0 is now way more likely than other numbers (a 1/537 chance instead of a 1/17million chance).

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Instead of relying on rand(), you could use the Mersenne Twister. Prelude has an implementation that will leave you eternally confuzzled.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    do a float divide (vs an integer divide) of the number you get from rand() by RAND_MAX, multiply it by the difference between your high and low, and then add your low.

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    This way you'd never get certain values (if RAND_MAX is smaller than desired range)?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  9. #9
    Registered User
    Join Date
    Jun 2008
    Posts
    266
    I not sure at all about this, but since RAND_MAX is just a #define, couldn't you just change the max value to the number you want?

  10. #10
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Quote Originally Posted by lruc View Post
    I not sure at all about this, but since RAND_MAX is just a #define, couldn't you just change the max value to the number you want?
    I don't think that will work. That would be like trying to change INT_MAX in order to change the range of an int. Those are more for reporting then an actual setting.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I not sure at all about this, but since RAND_MAX is just a #define, couldn't you just change the max value to the number you want?
    If I read the C99 standard correctly, that will result in undefined behaviour. In any case, the purpose of RAND_MAX is to show the upper limit of the range of the return value of rand(). Merely changing RAND_MAX without changing the implementation of rand() is asking for trouble.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    No. RAND_MAX is indicative of the capabilities of rand(), not a parameter.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Generating random letters
    By ominub in forum C Programming
    Replies: 6
    Last Post: 04-29-2009, 01:12 AM
  2. Generating random 2D mountains/terrain
    By Flaug in forum C++ Programming
    Replies: 7
    Last Post: 04-12-2009, 02:49 PM
  3. Making a random array of integers
    By Vidak in forum C# Programming
    Replies: 2
    Last Post: 11-09-2007, 06:00 AM
  4. Replies: 7
    Last Post: 09-26-2005, 05:09 PM
  5. Generating 1000 random integers
    By nizbit in forum C Programming
    Replies: 7
    Last Post: 12-09-2004, 06:40 PM