473,387 Members | 1,483 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How does srand() use time to generate random numbers?

Try to explain as simply as possible...I've only covered header files: <iostream>, <iomanip>, <cmath>, and just now being introduced to <ctime> and <cstdlib> (I think that's the header file name).
Dec 6 '09 #1
3 4153
donbock
2,426 Expert 2GB
srand() does NOT use time to generate random numbers.

The argument to srand() is an unsigned int. You can think of this argument as selecting a sequence of random numbers for successive calls to rand() -- a unique sequence for each value of the argument.

Sometimes you want to have the same sequence of random numbers each time your program runs. More often, you prefer a different sequence because that feels more ... random. To get a different sequence your program needs to pass a different value to srand() each time it runs. A convenient way to get a different value is by deriving one from the time-of-day that the program is executed. It is up to you to obtain/compute/derive the value passed to srand().
Dec 6 '09 #2
RRick
463 Expert 256MB
Srand is the random number generator that takes a seed value. Passing srand the time value is a good way to start a unique sequence of random numbers. If you want the random number sequence to be repeatable, then use the same integer value as a seed.

Typically, when creating a string of random numbers, you call srand first with the seed value, and then call rand for all of the rest of the random numbers. Try this link: http://www.cplusplus.com/reference/c.../cstdlib/rand/
Dec 8 '09 #3
Alright, i'll check the link out...and thanks for the explanations...
Dec 8 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Graeme | last post by:
I am writing a simple windows matching game with cards and pictures (ie "concentration"). In order to write something like this, I need to be able to give each "card" a random position in the...
0
by: August1 | last post by:
This is a follow up to using these functions to produce lottery numbers to an outfile, then read the numbers from the file to the screen. Although other methods are certainly available. ...
1
by: Wavelet | last post by:
If I use srand(value) to change the random seed, when I close the vc or run the code from the beginning, the seeds of random will change to default value or still keep the value of what I designed...
1
by: Intaek LIM | last post by:
generally, we use srand(time(0)) to generate random numbers. i know why we use time(0), but i can not explain how it operates. first, see example source below. ...
3
by: Ethan Strauss | last post by:
Hi, There have been quite a few discussions of Random not giving random numbers and how to fix that by feeding in a new seed each time, waiting enough time, or calling the Next() method of the...
3
by: sttip | last post by:
Hi, I'm a student taking a beginner's c++ course. I am trying to write a program that performes simple math operations on random numbers. I am using the srand(int(time(0))); function to generate...
9
by: Chelong | last post by:
Hi All I am using the srand function generate random numbers.Here is the problem. for example: #include<iostream> #include <time.h> int main() {
8
by: Ioannis Vranos | last post by:
Is srand(time(0)); an effective solution for seeding rand(), or is there any better approach?
31
by: Ioannis Vranos | last post by:
Regarding C95: Is srand(time(NULL)); an effective solution for seeding rand(), or is there any better approach?
12
by: Bill Cunningham | last post by:
I have read and studied and looked at references and can't find out how to do this. Here's where some real knowledge comes in now lets see who in clc really knows there stuff ;) main(void) {...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.