[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [ProgSoc] Randomisation
Warning: longish reply.
On Mon, 1 Feb 1999, Jay Banyer wrote:
[random stuff]
> index = rand() % num_structures;
>
> to pick the random index, and I have seeded the randomiser once before
> beginning the process using
>
> srand(time(NULL));
From my (admittedly limited) understanding, the method you are using is
fine, but the degree of randomness is dependant on the method of the
pseudo-random number generator you use. In this case you are using the
standard rand() function that comes bundled with whatever package you are
using.. (gcc? turbo c? etc.). See if you can find out how large its
operating space is.. i.e. how many numbers it will generate before it
starts repeating the sequence.. The larger the number, the more random
the numbers generated will be for any given seed value.
Basically, all the seed value does is kick off from a pseudo-random point
in the sequence possiblities given by the generator algorithm. The
problem with computers is that they are deterministic, so given the same
seed value, you'll get the same sequence of numbers from a given generator.
I've heard tell of generators which read the system things like space
between interrupts (keyboard etc) and small variations in the system
clock ticks to try to randomise the values a little more. I suggest a
quick look on the web for stuff on pseudo-random number generators and
check out how big their sequences are.
There's a reasonable replacement for standard rand() et al as part of the
circleMUD code, from memory. I vaguely recall reading the guy's comments
explaining why it was better than the standard rand().
A quick look at my RHlinux man page on rand gives a few useful literary
references to stuff on pseudo-random number generators and some handy hints:
"The versions of rand() and srand() in the Linux C Library
use the same random number generator as random() and sran-
dom(), so the lower-order bits should be as random as the
higher-order bits. However, on older rand() implementa-
tions, the lower-order bits are much less random than the
higher-order bits.
In Numerical Recipes in C: The Art of Scientific Computing
(William H. Press, Brian P. Flannery, Saul A. Teukolsky,
William T. Vetterling; New York: Cambridge University
Press, 1990 (1st ed, p. 207)), the following comments are
made:
"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)."
Random-number generation is a complex topic. The Numeri-
cal Recipes in C book (see reference above) provides an
excellent discussion of practical random-number generation
issues in Chapter 7 (Random Numbers).
For a more theoretical discussion which also covers many
practical issues in depth, please see Chapter 3 (Random
Numbers) in Donald E. Knuth's The Art of Computer Program-
ming, volume 2 (Seminumerical Algorithms), 2nd ed.; Read-
ing, Massachusetts: Addison-Wesley Publishing Company,
1981."
--
+---------------------------+-----------------------------------------+
| Justin Warren | justin.warren@nospam.its.maynick.com.au |
| Systems Administrator | daedalus@nospam.progsoc.uts.edu.au |
| Mayne Nickless Express IT | http://www.progsoc.uts.edu.au/~daedalus |
+---------------------------+-----------------------------------------+
| Just because you're paranoid doesn't mean they're NOT after you... |
+---------------------------------------------------------------------+
--
You are subscribed to the progsoc mailing list. To unsubscribe, send a
message containing "unsubscribe" to progsoc-request@nospam.progsoc.uts.edu.au.
If you are having trouble, ask owner-progsoc@nospam.progsoc.uts.edu.au for help.
This list is archived at <http://www.progsoc.uts.edu.au/lists/progsoc/>