# ofSeedRandom();

Hi guys acording to the documentation the ofSeedRandom(); is used to
“seed the random number generator to the clock time, so that random numbers will always be different.”

Im using
ofSeedRandom();
ofRandom(0, 100);

as i want to generate random numbers without repetition from 1 to 100. But the no repetition part is not working. Numbers are repeating before the possibilities end. Am i missunderstanding the use of ofSeedRandom(); or i’m using it in the wrong way?
Can anyone gimme a hand?
Maby i should use ofSeedRandom(); in the setup?

thanks for your help

[quote author=“daniel”]Hi guys acording to the documentation the ofSeedRandom(); is used to
“seed the random number generator to the clock time, so that random numbers will always be different.”

Im using
ofSeedRandom();
ofRandom(0, 100);

as i want to generate random numbers without repetition from 1 to 100. But the no repetition part is not working. Numbers are repeating before the possibilities end. Am i missunderstanding the use of ofSeedRandom(); or i’m using it in the wrong way?
Can anyone gimme a hand?
Maby i should use ofSeedRandom(); in the setup?

thanks for your help[/quote]

Simple pseudorandom number generators work by starting with a “seed” number that is fed into an algorithm mapping it to a binary number that is then scaled to the range you need. Every time you use it, the seed is incremented. There is no guarantee about the behavior of the generator from one call to the next.

One way to do what you want is use ofSeedRandom() in the setup, and create a vector numbers; containing the numbers 0-99. Then write a “wrapper” function that generates a random number ofRandom(0, numbers.size()) and then removes that element from the vector before returning the value.

another (similar) option is to “shuffle” an array – there is example code here:

http://www.fredosaurus.com/notes-cpp/mi-…-uffle.html

that could be helpful –

-z

Hi

Also found this problem, some days ago.

I think the problem is that ofSeedRandom is defined as:

void ofSeedRandom() {
// use time
srand(time(NULL));
}

the number returned from time(NULL) is a float with the number of seconds from 1970, so the difference between two calls is relatively so small that doesn’t affect the seeding.

Haven’t tested, but I suppose that taking for example the last 4 or 5 numbers from time(NULL) as seed or something similar, like getting minutes + seconds will work correctly.

The C library has been criticized for almost encouraging programming errors […] but this serious error can’t be blamed on the library. I think it’s rather the endless examples of using srand(time(NULL)) in many programming books that are to blame.

In that same page there’s a better way of seeding random, by using a xor between microseconds in the day, seconds in the day and the process id.

To change ofSeedRandom() just substitute the first lines in utils/ofMath.cpp with:

#include "ofMath.h"
#include <sys/time.h>

//--------------------------------------------------
int ofNextPow2(int a){
// from nehe.gamedev.net lesson 43
int rval=1;
while(rval<a) rval<<=1;
return rval;
}

//--------------------------------------------------
void ofSeedRandom() {
struct timeval tv;
gettimeofday(&tv, 0);

// use XOR'd second, microsecond precision AND pid as seed
long int n = (tv.tv_sec ^ tv.tv_usec) ^ getpid();
srand(n);

}

It’s surely standard c but if someone can test windows just in case it has some poxis specifics…

I had to replace getpid() with GetCurrentProcessId() and include windows.h

I think it should be seeded in the main before ofRunApp( new testApp());
and not in the setup() function.

When I seeded int the setup() i got the same numbers every time I executed the program.