Solved

random - Unix

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

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Which IDE to use to begin C++ training? 5 76
object oriented programming on screen browser tutorial lesson 2 96
What is sub-make ? 2 108
Finding Divisors 5 61
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 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.

710 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