## random number

### random number

Hi, I'm trying to get something like this

for i=1 to any number
{
a=random number generator between 1 and 2^16;
program a

Quote:}

I would appreciate any help, the idea is to run a C program that accepts
as an argument a number between 1 ant 2^16,     several times
Thank you

### random number

Quote:>Hi, I'm trying to get something like this

>for i=1 to any number
>{
>                a=random number generator between 1 and 2^16;
>                program a
>}

>I would appreciate any help, the idea is to run a C program that accepts
>as an argument a number between 1 ant 2^16,     several times
>Thank you

hopefully you have \$RANDOM otherwise you will have to make a function
for it. \$RANDOM should return a random number and use modulo operator
to get the range you want, in this case add 1 after so that the minimum
value is one. A counter can be maintained and incremented each loop
(I've used 'expr' here for maths but it's probably more likely that
you'll want to use '\$((' because that arithmetic evaluation is builtin
to the shell). Just check the counter until it gets to the maximum
value using a while loop ('for' loop in UNIX shell is very different
to the C version).

#!/bin/sh
COUNT=\$1          # set maximum value for loop count from command line parameter \$1
i=0                                   # \$i counts the number of loops so far
while [ \$i -ne \$COUNT ]               # repeat until we get to the maximum count
do
a=`expr \$RANDOM % 65536 + 1`        # set \$a to a random number s.t 1<=\$a<=65536
program \$a                          # invoke program with \$a as argument 1
i=`expr \$i + 1`                     # increment loop count by 1
done

(untested)

I recommend that you read your shell man page (man sh), all the
important building blocks of shell command language are well
documented in there and it pays to read it through (even a couple
of times over :)

ByeFrom

--
: \${L} # http://lf.8k.com:80

### random number

[...]
Quote:> while [ \$i -ne \$COUNT ]; do           # repeat until we get to the
>                                       # maximum count
>   a=`expr \$RANDOM % 65536 + 1`        # set \$a to a random number s.t
>                                       # 1<=\$a<=65536

[...] (I truncated the lines)

Note that \$RANDOM is a random number between 0 and 32767.
Also note that only modern shells (ksh, bash, zsh) have \$RANDOM, and
those shells have arithmetic evalution.

(( a = RANDOM / 16384 * 32768 + RANDOM ))

should work.

RANDOM / 16384 is better than RANDOM % 2

Note that you can emulate bash's RANDOM this way:
RANDOM=`expr \( \$RANDOM \* 1103515245 + 12345 \) % 32768 \)`

This explains why bash's RANDOM is once even, once odd, once even, once
odd... That's why bash's RANDOM lowest bit is not that random. That's
why RANDOM / 16384 is better than RANDOM % 2.

--
Stphane

### random number

September 2001 22:16:

> [...]
>> while [ \$i -ne \$COUNT ]; do           # repeat until we get to the
>>                                       # maximum count
>>   a=`expr \$RANDOM % 65536 + 1`        # set \$a to a random number
>>   s.t
>>                                       # 1<=\$a<=65536
> [...] (I truncated the lines)

> Note that \$RANDOM is a random number between 0 and 32767.
> Also note that only modern shells (ksh, bash, zsh) have \$RANDOM, and
> those shells have arithmetic evalution.

> (( a = RANDOM / 16384 * 32768 + RANDOM ))

> should work.

> RANDOM / 16384 is better than RANDOM % 2

> Note that you can emulate bash's RANDOM this way:
> RANDOM=`expr \( \$RANDOM \* 1103515245 + 12345 \) % 32768 \)`

> This explains why bash's RANDOM is once even, once odd, once even,
> once odd... That's why bash's RANDOM lowest bit is not that random.
> That's why RANDOM / 16384 is better than RANDOM % 2.

Shouldn't something like this solve the problem of a predictable
odd/even problem?
Using GNU bash:

MX="0123456789"
NL="5"
while [ \${n:=1} -le \$NL ]
do
NUM="\$NUM\${MX:\$((\$RANDOM%\${#MX})):1}"
let n+=1
done
echo "\$NUM"

Regards

Michael Heiming

Hi:

I am using random()&01 to get a binary random
number. Turns out if I generate more than 50 numbers,
the last 30 or more are all 1s or 0s, i.e., my random
number generator is not random!

Worse, if I generate 500 numbers, 497 are 1s.
The fraction of consistent numbers increases as the number
of trials increases.

Any clues? Anybody got a pointer to an FAQ where
I can get a random number generator that is random?