LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bash Random Number (https://www.linuxquestions.org/questions/programming-9/bash-random-number-511361/)

joelhop 12-17-2006 12:28 PM

Bash Random Number
 
Hey all,

I'm trying to generate a random number in bash:


#!/bin/bash

number=$RANDOM
echo $number

This works fine if you want a number in the 0 - 32767 (16-bit integer) range.

I need a larger number in the -2147483648 to 2147483647 (32-bit integer) range.

Is there another function like $RANDOM that provides a random number in this larger range. I've been looking for a while now and just keep turning up $RANDOM.

Thanks for any help,

-KARL

stress_junkie 12-17-2006 12:49 PM

You should have used the tags. I just explained this in detail and put the random+number tag on the post.

http://www.linuxquestions.org/questi...86#post2547786

Please use the site search features before creating a new post.

joelhop 12-17-2006 12:58 PM

Yeah, that totally doesn't answer my question. I did search before posting, and only found other hackish solutions like the one you referred me to. While that solution could possibly solve a similiar problem.

I am not looking for a loop that gives me any x number of 0-9 integers, of course I could do that.

My question is:
"Is there another function like $RANDOM that provides a random number in this larger range?"

-2147483648 to 2147483647 (32-bit integer)

There may not be, and the only solution may be to write your own, but I'd rather seek out built in first.

randyding 12-17-2006 01:34 PM

First, 0-32767 is only 15 bits.
You should take 3 $RANDOM numbers and shift bits to fill a 32 bit integer.
Code:

random32=$(( ( ($RANDOM & 3)<<30 | $RANDOM<<15 | $RANDOM ) - 0x80000000 ))

joelhop 12-17-2006 05:55 PM

http://en.wikipedia.org/wiki/16-bit
I suppose I should have specified +-32767.


Thank you for the reply, I like the direction you suggested. I'm still going to look around a bit for a built in function, although at this point it's looking bleak.

Hko 12-18-2006 06:35 AM

For random numbers of arbitrary length I once created this bash function:
Code:

#!/bin/bash

function random()
{   
    od -d /dev/urandom |\
    sed -e 's/^[0-9]* //' -e 's/ //g' |\
    while read L ; do echo -n $L ; done |\
    dd bs=1 count=${1:-10} 2>/dev/null
}

A=$(random)      # random number of 10 digits (default)
B=$(random 5)    # random number of 5 digits
C=$(random 5000) # almost unlimited number of digits works.

echo $A
echo $B
echo $C

Because the kernel's random generator is used the randomness is better than bash's internal generator (I assume).

On the downside this function is not very fast..

stress_junkie 12-18-2006 11:13 AM

Quote:

Originally Posted by randyding
First, 0-32767 is only 15 bits.
You should take 3 $RANDOM numbers and shift bits to fill a 32 bit integer.
Code:

random32=$(( ( ($RANDOM & 3)<<30 | $RANDOM<<15 | $RANDOM ) - 0x80000000 ))

and then subtract 2147483648. That would put the output in the correct range.

Oh wait. That's what the - 0x80000000 is about, isn't it?


All times are GMT -5. The time now is 07:19 AM.