Hi! i've taken this code straight from man rand it is supposed to generate random number between 1 and 10
j=1+(int) (10.0*rand()/(RAND_MAX+1.0));
now when I print j it *always* gives 9
i don't think this is random
i guess it has something to do with seed (what is it anywayz ?) when i call srand with different seed value it gives some other integer, but again it *always* gives that integer only..
thx -- Nikhil Joshi
On Thu, 3 Oct 2002, Nikhil Joshi wrote:
i guess it has something to do with seed (what is it anywayz ?) when i call srand with different seed value it gives some other integer, but again it *always* gives that integer only..
one generally calls srand with the current timestamp.
remember, one cannot generate random numbers by deterministic means. a classical computer is a deterministic engine and therefore *cannot* generate random numbers. They generate pseudo random numbers based on a randomising function (see Knuth). The function is dependent on the seed being sufficiently random. Picking the current millisecond value is a good choice.
Philip
nikhil,
Good point. Caveat: But based on philips advice two pieces of code. <sample> #include <stdlib.h> int main(){ int i; srand(time(0)); for(i=0; i<10;i++) printf("%d, ", 1+(int) (10.0*rand()/(RAND_MAX+1.0))); }</sample> <output> 6, 2, 4, 6, 10, 2, 8, 3, 5, 2, </output>
<bad sample> #include <stdlib.h> int main(){ int i; for(i=0; i<10;i++,srand(time(0))) printf("%d, ", 1+(int) (10.0*rand()/(RAND_MAX+1.0))); } </bad sample> <thus bad o/p> 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, <thus bad o/p> similar fundamental error in random no generation by netscape's ssl long ago..
hth, shailesh --- Philip S Tellis philip@konark.ncst.ernet.in wrote:
On Thu, 3 Oct 2002, Nikhil Joshi wrote:
i guess it has something to do with seed (what is it
anywayz ?) when i
call srand with different seed value it gives some
other integer, but
again it *always* gives that integer only..
one generally calls srand with the current timestamp.
remember, one cannot generate random numbers by deterministic means. a classical computer is a deterministic engine and therefore *cannot* generate random numbers. They generate pseudo random numbers based on a randomising function (see Knuth). The function is dependent on the seed being sufficiently random. Picking the current millisecond value is a good choice.
Philip
-- They have been at a great feast of languages, and stolen the scraps. -- William Shakespeare, "Love's Labour's Lost"
__________________________________________________ Do you Yahoo!? New DSL Internet Access from SBC & Yahoo! http://sbc.yahoo.com
Nikhil Joshi wrote:
Hi! i've taken this code straight from man rand it is supposed to generate random number between 1 and 10
j=1+(int) (10.0*rand()/(RAND_MAX+1.0));
now when I print j it *always* gives 9
Subsequent calls (in the same instance of the program) will not *always* yield 9. You're re-running the program with the same seed.
when i call srand with different seed value it gives some other integer, but again it *always* gives that integer
Again, subsequent calls with not give the same integer *always*. If you're only calling rand() once in your program and want it to give a diffirent integer each time then you can seed it with the current time --
srand((unsigned int) time(NULL)); j=1+(int) (10.0*rand()/(RAND_MAX+1.0));
Manish