How do I get random #s?

Mark H. Colburn mark at jhereg.Jhereg.MN.ORG
Fri Feb 3 10:41:10 AEST 1989


In article <19415 at dhw68k.cts.com> tsource at dhw68k.cts.com (Aryeh Friedman) writes:
>	I am new to C and I want to know how to get random numbers returned.


There are several different variations on how to get random numbers in C.

On most System V based systems and when using ANSI C you would use the 
function srand() to provide a random number seed (if you want one) and 
the rand() function to get a randon number back.

	unsigned int	seed;
	int		num;

	seed = ???;
	srand(seed);
	num = rand();


On most BSD derived systems you would use srandom() to provide the seen and 
the random() function to get the random number back.


	int		seed;
	long		num;

	seed = ???;
	srandom(seed);
	num  = random();

-- 
Mark H. Colburn                  "Look into a child's eye;
Minnetech Consulting, Inc.        there's no hate and there's no lie;
mark at jhereg.mn.org                there's no black and there's no white."



More information about the Comp.lang.c mailing list