[Subject Prev][Subject Next][Thread Prev][Thread Next][Subject Index][Thread Index]

Re: random numbers

your program is using the same seed value everytime it executes.checkout
"man rand" and use time function to get a random seed value.If you think
you would be using the funtion more than once within a second then use seed
as a static variable+timeofday.even if the funtion is recalled within a
second it will never have the same seed.obviously you will also do a seed++
in the function.

At 12:17 PM 06/14/2000 +0530, you wrote:
>Deepak Yadav wrote:
>> my program below generates same set of random numbers every time i run it. i
>> am using linux 6.2 and its random daemon is ON. plz help
>> #include<stdio.h>
>> #include<stdlib.h>
>> int main(int argc, char *argv[])
>> {
>>  char ch;
>>  int random;
>>  int i = 0;
>>  srand(1);
>>  while (i < 5 )
>>  {
>>    random = rand();
>>    printf("\nrandom : %d\n",random);
>>    i++;
>>   }
>> printf("\n\n");
>> return 0;
>> }
>you are seeding the random number generator always with 1. The random
>number generator generates Pseudo-Random Sequences. With the same seed,
>the same sequence will be generated.
>To get some random nos., u should seed the generator with something like
>say f(timeofday).
>the man page for random treats this *quite* exhaustively!
>The LIP mailing list archives are available at: