## Generating Random Numbers

### Generating Random Numbers

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

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

> >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

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>

How can I generate a random number using a shell script or would I be
better off using some language to do that kind of thing?

Is there a FAQ for this place? Where is it?

Thanks for any info.

-Z