[comp.lang.c] randomness

sicolo@acsu.Buffalo.EDU (james sicolo) (04/09/90)

	What function must one use to get different sequences of random
numbers each time the program is run????

	By just using random(), I get the same sequence each time.  Is there
another function to use ??

							Jim

john@stat.tamu.edu (John S. Price) (04/09/90)

In article <21274@eerie.acsu.Buffalo.EDU> sicolo@acsu.Buffalo.EDU (james sicolo) writes:
>
>	What function must one use to get different sequences of random
>numbers each time the program is run????
>
>	By just using random(), I get the same sequence each time.  Is there
>another function to use ??
>
>							Jim


You need to seed your random number generator.  If you are
using rand(), use srand() to seed the rrandom number generator.
Send srand() some value that you will know change each time
you run the program, like time(NULL).  So, 

...

srand(time(NULL));
...
x = rand();   

...

That's how I do it, anyway...


--------------------------------------------------------------------------
John Price                   |   It infuriates me to be wrong
john@stat.tamu.edu           |   when I know I'm right....
--------------------------------------------------------------------------

kla@physc1.byu.edu (04/10/90)

One easy way to get a different sequence each time the program is run
is to set the seed to the clock. Different C compilers have different
ways of setting the seed so you will have to consult your documentation.

scs@athena.mit.edu (Steve Summit) (04/11/90)

In article <4878@helios.TAMU.EDU> john@stat.tamu.edu (John S. Price) writes:
>srand(time(NULL));

Make that

	#include <sys/types.h>
	srand((int)time((time_t)NULL));

This is not mere pedantry; it does make a difference.  I'm
posting this reminder only because this code matches the one time
I ever got burned by a realio, trulio int/pointer mismatch, and
in case there is anyone out there who still isn't convinced that
proper pointer casts are in fact necessary in function call
arguments.  Somehow I forgot the cast one day and typed
srand(time(NULL)) into the planet's lowest quality/popularity
ratio computer (the PC) and compiled using a "memory model" in
which ints and pointers are of different size.  Since the
designers of that machine apparently believed that protection
mechanisms were only necessary in the antique mainframe computers
which the PC revolution repudiated, the result of this mistake
(on the PC) is not "Segmentation violation (core dumped)" but
rather a hung machine, which must be power-cycled and rebooted.

Watch those NULL pointers, folks.  (Yes; prototypes might have
helped.)
                                            Steve Summit
                                            scs@adam.mit.edu

scs@athena.mit.edu (Steve Summit) (04/12/90)

In article <1990Apr11.001355.25937@athena.mit.edu> my evil twin Skippy wrote:
>Make that
>	srand((int)time((time_t)NULL));

This should, of course, be

	srand((int)time((time_t *)NULL));

Forty lashes with a wet noodle for me for posting a code fragment
I didn't compile and lint.

Thanks to Mark Brader for pointing this out so kindly, and to the
rest of you for not ridiculing me (yet...)