?
Solved

C++ Real rnadom numbers

Posted on 2005-03-20
6
Medium Priority
?
674 Views
Last Modified: 2008-01-09
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
Comment
Question by:liseperu
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 48

Expert Comment

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

Author Comment

by:liseperu
ID: 13585133
here is my code:

double random = 0.0;

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

Expert Comment

by:AlexFM
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:liseperu
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

by:
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

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

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses
Course of the Month14 days, 22 hours left to enroll

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question