Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > pass time(0) to srand() when generating random numbers.

Reply
Thread Tools

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

 
 
Intaek LIM
Guest
Posts: n/a
 
      10-31-2003
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.
 
Reply With Quote
 
 
 
 
Andreas Kahari
Guest
Posts: n/a
 
      10-31-2003
In article <(E-Mail Removed) >, 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Math.random() and Math.round(Math.random()) and Math.floor(Math.random()*2) VK Javascript 15 05-02-2010 03:43 PM
random.random(), random not defined!? globalrev Python 4 04-20-2008 08:12 AM
Random number generator, generating 10 different numbers. Need Help. Wally ASP .Net 1 03-20-2006 12:19 AM
Random Number Generating Leon ASP .Net 6 10-18-2004 05:56 PM
Support for generating random passwords in ASP.NET =?Utf-8?B?Sm9l?= ASP .Net 2 08-04-2004 10:37 PM



Advertisments