473,378 Members | 1,427 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,378 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 30860
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: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...

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.