[comp.sys.next] random number generator

matthews@is-next.umd.edu (Mike Matthews) (12/29/90)

I'm trying to make a random sound player for my NeXT, using a shell script and
crontab.local.  I have everything working just fine, except for one small thing:
the random numbers aren't really random.  They always start at the same thing.
Now, I know a little about how this works, and I know it has something to do
with setting the seed (or the state -- I'm using the newer random() function),
but how can I set *that* randomly every time the script is run?

I essentially do a set num=`random <lo mark> <hi mark>`, then a
/usr/bin/sndplay /usr/local/snds/$num.snd, which works, but it always plays
the same sound...

Any help would be appreciated.  I was thinking I could just do a srandom(x)
but that would still be a "fixed" random marker, wouldn't it?  Please reply by
Email to matthews@is-next.umd.edu, and I will post the answer if/when I get it.

Thanks again,
Mike

glenn@heaven.woodside.ca.us (Glenn Reid) (01/02/91)

In article <7781@umd5.umd.edu> matthews@is-next.umd.edu (Mike Matthews) writes:
>the random numbers aren't really random.  They always start at the same thing.
>Now, I know a little about how this works, and I know it has something to do
>with setting the seed (or the state -- I'm using the newer random() function),
>but how can I set *that* randomly every time the script is run?

You're right; they're actually called "pseudo-random" number generators,
because the pattern of numbers they generate is not likely to repeat in
any observable patterns, but they're not really random.

The usual approach is to seed the random number generator with the
number of ticks of the system clock, which can be obtained with the
"time()" system call.  That number is very much what you or I would
consider "random" since it changes once a second, but you can't keep
sampling the time in your loop because the sequence will not be
random.

Try something like this (although I haven't tested it):

	long your_random_number;

	srandom ( (int)time(0) );
	your_random_number = random();

I hope this helps.

Glenn
-- 
 Glenn Reid				RightBrain Software
 glenn@heaven.woodside.ca.us		PostScript/NeXT developers
 ..{adobe,next}!heaven!glenn		415-851-1785