[comp.unix.questions] random numbers in a C shell script?

gefuchs@skolem.uucp (Gill E. Fuchs) (05/04/89)

Bon Jorno net,

how would one go about getting a random numba in a C shell script?

muchas randomas gracias

randy@uokmax.UUCP (Longshot) (05/06/89)

In article <1501@cmx.npac.syr.edu> gefuchs@logiclab.cis.syr.edu (Gill E. Fuchs) writes:
-
-Bon Jorno net,
-
-how would one go about getting a random numba in a C shell script?
-
-muchas randomas gracias

No way that I could find. I wrote this tiny prg to allow me to do it:


#include <stdio.h>
/*
	Return a random number between 1 and the argument given.
*/
main(argc,argv)
register int argc;
register char **argv;
{
	int i;
	long time(),random();

	srandom(time(0));
	i = atoi(*++argv);
	i = ((int)random() >> 4) % i;
	printf("%d\n",++i);
}

I use it to randomly select a .signature when I log in.

Randy
-- 
Randy J. Ray       University of Oklahoma, Norman, Oklahoma	(405)/325-5370
!texsun!uokmax!randy	randy@uokmax.uucp    randy@uokmax.ecn.uoknor.edu
"No one knows what it's like... to be the the bad man, to be the sad man..
behind blue eyes..."	-The Who

cudcv@warwick.ac.uk (Rob McMahon) (05/08/89)

In article <3138@uokmax.UUCP> randy@uokmax.UUCP (Longshot) writes:
>In article <1501@cmx.npac.syr.edu> gefuchs@logiclab.cis.syr.edu (Gill E. Fuchs) writes:
>-how would one go about getting a random numba in a C shell script?
>
>No way that I could find. I wrote this tiny prg to allow me to do it:

4.3BSD comes with a really useful program called `jot' in the user contributed
software under `tools'.

	jot -r 3 10 99

will give 3 random numbers between 10 and 99 inclusive

	jot -c - a z 1

prints the alphabet.  I use it all the time in e.g.

	foreach i ( `jot -c - a z 1` )

Rob
-- 
UUCP:   ...!mcvax!ukc!warwick!cudcv	PHONE:  +44 203 523037
JANET:  cudcv@uk.ac.warwick             ARPA:   cudcv@warwick.ac.uk
Rob McMahon, Computing Services, Warwick University, Coventry CV4 7AL, England

dg@lakart.UUCP (David Goodenough) (05/08/89)

gefuchs@skolem.uucp (Gill E. Fuchs) sez:
> Bon Jorno net,
> 
> how would one go about getting a random numba in a C shell script?
> 
> muchas randomas gracias


#! /bin/csh

@ rng = `date | tr : ' ' | awk '{ print $3 * $4 * $5 * $6}'` + $$

echo $rng

Gives "reasonable" oneshot random numbers. If you need a mess of them,
then take $rng, and a LCG (did I get the term right???):

@rng = $rng * something + something_else

which might keep you happy. Failing this, get on with Knuth Volume 2 and
awk.
-- 
	dg@lakart.UUCP - David Goodenough		+---+
						IHS	| +-+-+
	....... !harvard!xait!lakart!dg			+-+-+ |
AKA:	dg%lakart.uucp@xait.xerox.com		  	  +---+

bink@aplcen.apl.jhu.edu (Ubben Greg) (05/17/89)

In article <540@lakart.UUCP> dg@lakart.UUCP (David Goodenough) writes:
> ...
> @ rng = `date | tr : ' ' | awk '{ print $3 * $4 * $5 * $6}'` + $$
> ...

Not being qualified to play with the algorithm, I present instead
another way of doing the SAME thing (in Bourne shell):
	rng=`date +"%d %H*%M*%S*$$+p" | dc`
You'll probably want to throw in a MOD too.

Another idea:
	combine in the output of a TIME or TIMEX (something like
	"timex date 2>&1 | sed ... | dc")

Also, I'm not good at csh, but can't you just command substitute a
date +"..." right into the @ instead of making awk do the math?
Something like
	@ rng = `date +"%d * %H * %M * %S"` + $$
maybe with an EVAL thrown in for good measure?

						-- Greg Ubben
						   bink@aplcen.apl.jhu.edu