Random Numbers :: C++

Status
Not open for further replies.
Hi.

I am experience recursive random number. It seems that only happens when rand() is in a loop.

For example:

--------------------------------------
srand(time(0));
int tempNUM = 0;

for (int i = 0; i < 1000; ++i)
{
tempNUM = 1 + rand() %9999999;
cout << tempNUM + 1;
}
--------------------------------------

In a program I am working on with a similar code to the one above, the program generates duplicates random numbers. It seems I have to *wait* one second clock until it generates a something new. Does that have something to do with time(0)?

I would like something that can generate a completely random number almost instantaneously even when it is in a for loop like the one above.

Thanks,
Kuphryn
 
There are plenty of things out there better than rand() (which most likely returns numbers < 65535 BTW). Search the net for "random number generator". I recall having seen one with very high period and great distribution that used floating point calculations... but my HD's crashed since then :(

That said, rand() still shouldn't have the behavior you describe. You're not reseeding it every time in the loop are you? Doesn't look like it from the code you posted... but make sure you're only calling srand once in your program. Otherwise, rand would reset itself to the same number every time for a whole second since time() changes every second.
 
Thanks everyone.

bash666: Hey, nice link to good information.

I have some questions about the formula I saw at the second website you posted.

-------------------------------------------------------------------
r = ( (double)rand() / (double)(RAND_MAX+1) );

/* r is a random floating point value in the range [0,1) {including 0, not including 1}. Note we must convert rand() and/or RAND_MAX+1 to floating point values to avoid integer division. */
-------------------------------------------------------------------

What is the "double" stand for in terms of syntax? I do not quite understand how to implement "(double)rand()" into C++. Is that like this?

r = (static_cast<float> (rand()) / static_cast<float> (RAND_MAX + 1));

Second, how does "(int)" relate to C++ in this equation?

-------------------------------------------------------------------
y = (int) x;

* y is a random integer in the range [0,M) {including 0, not including M}. If M is an integer then the range is [0,M-1] {inclusive} */
-------------------------------------------------------------------

y = static_cast<int> (x);

Is that what is means?

Kuphryn
 
I did not call srand() inside the for-loop, but I did call it at the beginning of a function that I think could be recursive.
Well that's pretty equivalent :) The instructions at the beginning of a recursive function get called over and over and over and over... You could use something like this to be safe (if putting srand in the calling function doesn't make sense for whatever reasons):
code:<HR>
Code:
void CallsrandOnce()
{
    static bool srandDone = false;
    
    if (!srandDone)
    {
        // call srand here
        srand(time(0));
        // set flag so it isn't called again
        srandDone = true;
    }
}
You can then use this function where you need to be sure that srand will have been called at least once.
 

Scrod

Ars Scholae Palatinae
1,362
Originally posted by stickboy:
What about those thermal-noise-based random number generators that P3s have? Are there libraries that make of it?

Well, I know that it's possible to use /dev/intel_rng on Linux to generate random numbers with the help of the rngd daemon. At least, that's what I've done on this Debian box that I've got next to me.
 
Status
Not open for further replies.