Quick but effective Random

Hey all,

What is a simple but relatively sophisticated random function that I could easily embed into a project? Thanks in advance.
There's c++ built-in rand() function.

To use it, first "seed" it:
1
2
3
4
5
//put this with all your other include declarations
#include <ctime>
...
//stick this near the beginning of your code. The first line of main() should do it.
srand ( time(NULL) );


Then any time afterwards, you can call rand() to get a random number between 0 and RAND_MAX, which is always at least 32767. If you want a smaller range, you can use the modulus operator:

1
2
3
int upperLimit = 10;
int myNumber = rand() % upperLimit;
//myNumber is now somewhere between 0 and 9 
Thanks - I was actually hoping for something a little more random than what was provided by rand(), but my own fault for not specifying. Thank you for posting all the same!
I see. Well, you could check out the Marsenne Twister, which is pretty popular:
http://en.wikipedia.org/wiki/Mersenne_twister

...But I've never used it before, so I wouldn't know how to implement it.
I found an MT header on the net, works great. Thanks for your help!
Topic archived. No new replies allowed.