Solved

random - Unix

Posted on 1997-04-29
1
195 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now