473,396 Members | 1,725 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,396 software developers and data experts.

pass time(0) to srand() when generating random numbers.

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.

---------------------------------------------
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
printf("This will generate 5 random numbers.\n\n");

srand(time(0));

int i;
for(i=0; i<5; i++)
{
printf("generated %d\n", rand());
}

return 0;
}
---------------------------------------------

this is an idiom of generating random numbers, i think.
and a different source below operates correctly too.

---------------------------------------------
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
printf("This will generate 5 random numbers.\n\n");

int seed = time(0);
srand(seed);

int i;
for(i=0; i<5; i++)
{
printf("generated %d\n", rand());
}

return 0;
}
---------------------------------------------

but, following source produces incorrect results.
(returns the same values everytime)
---------------------------------------------
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
printf("This will generate 5 random numbers.\n\n");

// seed is the return value for time(0)
// i wrote it by my hand. :-(
int seed = 1067589554;
srand(seed);

int i;
for(i=0; i<5; i++)
{
printf("generated %d\n", rand());
}

return 0;
}
---------------------------------------------

what is the difference of these three sources?
plz tell me something who knows it.
Nov 13 '05 #1
1 30869
In article <d9**************************@posting.google.com >, Intaek LIM wrote:
[cut]
// seed is the return value for time(0)
// i wrote it by my hand. :-(
int seed = 1067589554;
srand(seed);


The time() function returns a value that represents the current
time. Seeding the pseudo random generator with time(0) will
ensures that you get a different pseudo random list of numbers
each time you run the program.

Hard coding the seed ("1067589554" in your code) ensures that
you get the *same* pseudo random list of numbers each time you
run your program.

--
Andreas Kähäri
Nov 13 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

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...
2
by: deanfamily | last post by:
I have a double-linked list. I need to insert RANDOM numbers into it. The rand() function doesn't put "random" number in the list, in the sense that every time you run the program it should...
1
by: Velhari | last post by:
Hi, I am a beginner. Please tell me, For generating Random Numbers, Initially why we are going for seed method. And another question is that, I want to print unique random number how to print by...
16
by: Markus Dehmann | last post by:
According to several C++ tutorials, calling srand like this to initialize the random number generator seems to be standard: srand((unsigned)time(0)); But it leads to the same random number...
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() {
16
by: conrad | last post by:
What kind of problems could arise if one were to not cast the value of time() to unsigned int? What are the sections in the standard that provide the reasoning of any claims against such a...
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?
23
by: Bill Cunningham | last post by:
I have no idea what to do with time () or another function to change the time_t data type to int to get numbers say from 0-20 that are ints. time() reports a huge number of seconds and when using...
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: 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
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: 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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.