Solved

random - Unix

Posted on 1997-04-29
1
197 Views
Last Modified: 2010-04-10
I have some problems with generating random numbers
with C++ on UNIX (GNU).
-  srand, rand        : the numbers are always the same.
-  srand48, drand48   : not a success with me!
Do you know more about it ?
0
Comment
Question by:Chiron
1 Comment
 
LVL 3

Accepted Solution

by:
LucHoltkamp earned 40 total points
ID: 1163023
rand is a PSEUDO-random generator and you should call srand to initialize the random generator.

I can think of two things that go wrong:
2) You call srand always with the same number. Try to use:
srand((unsigned)time());

1) You call srand before EVERY call to rand. That way the random number generator is initialised every time (with the same number if you use time(), and there is a short interval between every call).
Try to call srand only once, at the start of your program:
srand((unsigned)time());

Hope that helps, Luc
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Article by: SunnyDark
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…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
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.

772 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