[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: urgent problem
Chaim Zucker <zuckerc@macs.biu.ac.il> writes:
> i know this is not quite the place to ask for this .... but
> i have (in c++) a function that uses rand() (to print a matriza) i have to
> give the user the option to print it as many times as he likes but
> everytime the function is calles rand() comes up with different numbers
> what can i do to keep the same numbers coming up?
> i am using solaris/SunOs on sparc/hds/pc and using the function:
> rand() % 100
> p.s: it is really urgent
> thanx
Give the generator the same seed (man srand).
I am not sure what rand() % 100 is supposed to do, but look for
the correct way to produce "random" numbers between a and b. Oh,
what the hell, I'll put it here (the man page quotes Numerical
Recipes, which is not always a good source, but I guess it's OK
here).
"If you want to generate a random integer between 1
and 10, you should always do it by
j=1+(int) (10.0*rand()/(RAND_MAX+1.0));
and never by anything resembling
j=1+((int) (1000000.0*rand()) % 10);
(which uses lower-order bits)."
If you need random numbers of decent quality (i.e. it's not a toy
program), do not use rand().
It is somewhat off-topic (C++ on solaris, you say).
--
Oleg Goldshmidt goldshmt@netvision.net.il
BLOOMBERG L.P. (BFM) oleg@bfr.co.il