[net.lang.c] Random Numbers

ksl@hou2e.UUCP (a hacker) (07/18/85)

While I am not a beginner, I am not really experienced, in a sence.
I am still mystified upon how you can create a fairly random number
generator.  I mean, as long as the program generates different
numbers each time.  Something like this would be sufficient (on a greater
scale):
	$ a.out		$ a.out		$ a.out
	3		1		2
	2		3		1
	1		2		3
	2		1		3
	1		3		2
	3		2		1

As you see, it is really just pseudo random, but there is no such
thing as really random numbers from a computer.
[ Pick a memory location, any memory location. ]

Can anyone help?   

Thanks, hou2e!ksl

Please respond by mail.  Thanks.

gwyn@BRL.ARPA (VLD/VMB) (07/21/85)

You are right -- there is no way to generate "really" random
numbers in a deterministic manner.  Indeed, defining what is
meant by "random number" is not easy.  The classic reference
for these matters is Donald Kunth's "The Art of Computer
Programming, Vol. 2: Seminumerical Algorithms", 2nd. Ed.
(Addison-Wesley, 1981).

Most people in practice use a library function like rand()
and "seed" it (via srand()) with a number unique to the
process invocation, such as the XOR of the time-of-day and
the process ID.  Be warned that most implementations of
rand() do not generate very good random sequences!

chen@acf4.UUCP (H. Chen, Nick Jacobs) (07/22/85)

Recently a friend and I needed some random numbers for a game (sigh) program.
Since we just wanted to generate a small range of values, we simply made an
array with the range of values in it. Since we knew that the user's "access"
to these psuedo-random values would be semi-random in itself; we simply creat-
ed a mini Turing machine algorithm (sort of) where the value returned would be 
dependent on the state of the algorithm from a prior call. (It was like a Turing
machine in that we moved up and down the array depending upon the previous state
of the algorithm.) 

We haven't had a chance to test this algorithm heavily, but the game (for the
PC) was like pong in which the ball's movement is already semi-random due to
the fact that most people cannot control the ball that well.

					Nicholas Jacobs
					chen@acf4 (just for the summer)
					...cmcl2!acf4!chen

werner@aecom.UUCP (Craig Werner) (09/05/85)

	What is the best way to generate random numbers in C.
	The application in question is to extract a random line of a file.

	(I realize that this has been done in programs such as 'fortune')
-- 
				Craig Werner
				!philabs!aecom!werner
		"The world is just a straight man for you sometimes"

john@frog.UUCP (John Woods) (09/09/85)

> 
> 	What is the best way to generate random numbers in C.
> 	The application in question is to extract a random line of a file.
> 
> 	(I realize that this has been done in programs such as 'fortune')
> -- 
>
Glad you asked.  Knuth's The Art of Computer Programming, Volume 2, Section
3 "Random Numbers" is a good place to start.  I doubt that there is a One
Best Way, but for your amusement and entertainment, I supply my own C
implementation of Algorithm A (Additive number generator).  Knuth likes this
algorithm, but says "there is still very little theory to prove that it does
or does not have desirable randomness properties", but that in practice, it
has appeared to work.
---------slice,-dice,-chop,-grate,-and-cut-here---------------------------
/*
 *	Knuth's 55-number random number generator
 *	Algorithm A (Additive number generator)
 *	p27, S 3.2.2, Volume 2 of The Art of Computer Progamming
 */

/* 24 and 55 are not just Random Numbers, but are Sacred Numbers of the
 * algorithm, hence I have not #defined them, lest a nonbeliever be led astray
 */

static int RANDU[55] = {	/* a name of power */
	/* insert 55 of your own favorite random numbers here, not all
	   even.  Due to varying "int" lengths, I am not providing any.
	   Calling the traditional rand() 55 times is probably sufficient.
	*/
};
static int *X24 = &RANDU[23], *X55 = &RANDU[54];

rand() {
	int rv;
	rv = *X55 = (*X55 + *X24--);
	if (X24 < &RANDU[0]) X24 = &RANDU[54];
	if (--X55 < &RANDU[0]) X55 = &RANDU[54];
	return rv;
}

srand(n) {
	*X55 = n;
}

--
John Woods, Charles River Data Systems, Framingham MA, (617) 626-1101
...!decvax!frog!john, ...!mit-eddie!jfw, jfw%mit-ccc@MIT-XX.ARPA

"Out of my way, I'm a scientist!"
			- War of the Worlds