Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
Solved

Posted on 2005-03-20
Medium Priority
682 Views
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.
0
Question by:liseperu
• 3
• 3

LVL 48

Expert Comment

ID: 13585129
srand( time( NULL ) ) is the way to initialize random time generator. Please show your code.
0

Author Comment

ID: 13585133
here is my code:

double random = 0.0;

srand(time(NULL));
random = (rand()%4);
0

LVL 48

Expert Comment

ID: 13585151
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 Comment

ID: 13585263
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

LVL 48

Accepted Solution

AlexFM earned 2000 total points
ID: 13585279
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

Author Comment

ID: 13585609
yep, that did it... thanks a lot for your help
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
###### Suggested Courses
Course of the Month15 days, 11 hours left to enroll