Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

random - Unix

Posted on 1997-04-29
1
Medium Priority
?
203 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
[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
1 Comment
 
LVL 3

Accepted Solution

by:
LucHoltkamp earned 80 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

670 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