[Subject Prev][Subject Next][Thread Prev][Thread Next][Subject Index][Thread Index]
Re: random numbers
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!
sachin