Generating Random Numbers

Generating Random Numbers

Post by Calvino Ka-wing Che » Thu, 23 Jan 1997 04:00:00



Hi!  I just stumbled across this newsgroup and was wondering if I
could get some help.

I would like to have a random number generator (run via crontab
every night) which generates a random number between 1-100
(note: the number can also be 234-334.... as long as 100 - or any
whole number - or so digits separate them)

Can it be done on the unix shell?

Thanks.  Help, and perhaps a few lines of code would be appreciated
(e-mail me back)

Cal

--

****************************            
Calvino Cheng                           University of Calgary
Biochemistry Student

 
 
 

Generating Random Numbers

Post by Bob Sha » Thu, 23 Jan 1997 04:00:00



Quote:>Hi!  I just stumbled across this newsgroup and was wondering if I
>could get some help.

>I would like to have a random number generator (run via crontab
>every night) which generates a random number between 1-100
>(note: the number can also be 234-334.... as long as 100 - or any
>whole number - or so digits separate them)

>Can it be done on the unix shell?

Well, it's easy to do in Perl.  awk may be as easy, but I don't do awk.
$ cat try
#!/usr/local/bin/perl
srand(time|$$);
$num = int(rand(100)) + 1;
print "$num\n";
$ try
69
$ try
46
$ try
97

--
Bob Shair                          Open Systems Consultant

Champaign, IL 61821                217/356-2684
< Not employed by or representing the University of Illinois >

 
 
 

Generating Random Numbers

Post by Brian Graha » Thu, 23 Jan 1997 04:00:00




> >Hi!  I just stumbled across this newsgroup and was wondering if I
> >could get some help.

> >I would like to have a random number generator (run via crontab
> >every night) which generates a random number between 1-100
> >(note: the number can also be 234-334.... as long as 100 - or any
> >whole number - or so digits separate them)

> >Can it be done on the unix shell?

> Well, it's easy to do in Perl.  awk may be as easy, but I don't do awk.
> $ cat try
> #!/usr/local/bin/perl
> srand(time|$$);
> $num = int(rand(100)) + 1;
> print "$num\n";
> $ try
> 69
> $ try
> 46
> $ try
> 97

> --
> Bob Shair                          Open Systems Consultant

> Champaign, IL 61821                217/356-2684
> < Not employed by or representing the University of Illinois >

Here is a simple korn shell way to do it.

#!/bin/ksh
magicnum=$(($RANDOM%100+1))
echo $magicnum

--

                Brian Graham

 
 
 

Generating Random Numbers

Post by Michael Staa » Fri, 24 Jan 1997 04:00:00



Quote:>I would like to have a random number generator (run via crontab
>every night) which generates a random number between 1-100
>(note: the number can also be 234-334.... as long as 100 - or any
>whole number - or so digits separate them)
>Can it be done on the unix shell?

It can be done easily in ksh.

$(( RANDOM / 328 + 1 ))

gives you random numbers between 1 and 100.
Bye,
        Michael
--
Michael Staats, Theoretical Physics, Uni-GH Duisburg

<a href="http://WWW.thp.Uni-Duisburg.DE/">Click</a> me!
<a href="http://WWW.thp.Uni-Duisburg.DE/cuaix/cuaix.html">A c.u.aix archive</a>