ofSeedRandom() problem ...

Hello!

I have a problem with generating random numbers in OF. I tried this:

  
  
#include "myClass.h"  
  
myClass::myClass()  
{  
  
ofSeedRandom();  
  
x = ofRandom(100, 500);  
cout << x << endl;  
  
}  

Which only partially works … it gives numbers in the right range, but if I make more than one instance of this object, the numbers stay very close together. Anyone know of any reason for that? (Actually, I think I know why this happens, so the correct question should probably be; Does anyone know how to fix this?) …

:slight_smile: cheers

here is a guess :

I’m not sure what you are seeing, but you don’t need to seed the random number generator every time. ofSeedRandom is using the current time to seed unless you pass in a fixed value (so that that every time you start the application, you get different results) but if you seed every time you make a new object, the seed values are close to each other so the random values will be close.

just call the seed function once at the start of the app (in testApp::setup()) – actually, we do seed in OF at the start to time, so unless you are trying to use the seed to get the same random results, every time you run the app, you don’t need to call it. If you do want to get the same random results every time seed it to a fixed value and not to time at the start of your app.

hope that helps!
zach

Thanks for your reply! Yes, I thought that would be the problem, but I have tried different ways of getting this to work, but with no luck. Could it be that I initialize the objects wrong? I’m used to initialize objects/instances with the ‘new’ command, but that is not how it is done here, right? I’m not using any pointers or arrays either, just ‘declaring’ my objects in the header file, and as far as I understand this, that is when the objects are being created(?) … Anyways, when I put the ofSeedRandom() in the setup, I do get the same result each time. Are the objects being created before the setup and ofSeedRandom are called in this way? I’ll take another look at the Ooooops guide as well :slight_smile: