C++ Real rnadom numbers

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.
liseperuAsked:
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.

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

double random = 0.0;

srand(time(NULL));
random = (rand()%4);
0
AlexFMCommented:
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
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

liseperuAuthor 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
AlexFMCommented:
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

Your issues matter to us.

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

Start your 7-day free trial
liseperuAuthor 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.