Solved

random - Unix

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
maze travler 6 51
why "." vs "->" 23 119
Fast, free, private online code repository / editor 7 90
Unable to start eclipse ? 17 128
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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 goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

929 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

14 Experts available now in Live!

Get 1:1 Help Now