Hi,
I need to generate a random number between two values. So far I have been using rand() but it's not actually random, I always get the same pattern which is causing inaccuracies in my program. I tried seeding srand(time(NULL)) but that causes the same number to be generated every time... I assume because my program runs to fast for time() to keep up?

anyway, I need a way of generating a real random number...

thanks.
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
srand( time( NULL ) ) is the way to initialize random time generator. Please show your code.
0
Author Commented:
here is my code:

double random = 0.0;

srand(time(NULL));
random = (rand()%4);
0
Commented:
I run the following program number of times and get different numbers from 0 to 3:

double random = 0.0;
srand(time(NULL));
random = (rand()%4);
cout << random << endl;

I guess you run this code in some loop and get the same results, like this:

for ( int i = 0; i < 10; i++ )
{
double random = 0.0;
srand(time(NULL));
random = (rand()%4);
cout << random << endl;
}

Solution is to call srand only once before loop:

srand(time(NULL));

for ( int i = 0; i < 10; i++ )
{
double random = 0.0;
random = (rand()%4);
cout << random << endl;
}
0
Author Commented:
sorry, I should have put the whole function:

Neuron::Neuron(int noInputs, double Xcoord, double Ycoord, int noWeights,vector<vector<double> > data)
{
_noInputs = noInputs;
_Xcoord = Xcoord;
_Ycoord = Ycoord;

_noWins = 0;
_winRate = 0;

double random = 0;
srand(time(NULL));
for(int x = 0; x< noWeights; x++)
{
random = (rand()%254)+1;
_weights.push_back(0.0);
}

}

however, this is in the constructer of a class that is created in a for loop in another class . i.e.

for(int t = 0; t < _numberOfNeuronsPerLayer; t++)
{
_neuronLayer.push_back(Neuron(noInputs, 0, t, noWeights,data));
}

which is created in a loop in another class!!

How about if I seed srand in main before I start the actual system?
0
Commented:
Generally, random generator is seed once per program running. This ensures that initial value returned by rand function is always different. After initialization numbers produces by rand function may be thought as real random numbers. Try to call srand from main, hopefully this will help.
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author Commented:
yep, that did it... thanks a lot for your help
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.