LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-17-2006, 12:28 PM   #1
joelhop
Member
 
Registered: Mar 2004
Location: Pennsylvania::USA
Distribution: Fedora Core 6
Posts: 100

Rep: Reputation: 15
Question 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
 
Old 12-17-2006, 12:49 PM   #2
stress_junkie
Senior Member
 
Registered: Dec 2005
Location: Massachusetts, USA
Distribution: Ubuntu 10.04 and CentOS 5.5
Posts: 3,873

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
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.
 
Old 12-17-2006, 12:58 PM   #3
joelhop
Member
 
Registered: Mar 2004
Location: Pennsylvania::USA
Distribution: Fedora Core 6
Posts: 100

Original Poster
Rep: Reputation: 15
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.

Last edited by joelhop; 12-17-2006 at 01:02 PM.
 
Old 12-17-2006, 01:34 PM   #4
randyding
Member
 
Registered: May 2004
Posts: 552

Rep: Reputation: 31
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 ))
 
Old 12-17-2006, 05:55 PM   #5
joelhop
Member
 
Registered: Mar 2004
Location: Pennsylvania::USA
Distribution: Fedora Core 6
Posts: 100

Original Poster
Rep: Reputation: 15
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.

Last edited by joelhop; 12-17-2006 at 06:00 PM.
 
Old 12-18-2006, 06:35 AM   #6
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
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..
 
Old 12-18-2006, 11:13 AM   #7
stress_junkie
Senior Member
 
Registered: Dec 2005
Location: Massachusetts, USA
Distribution: Ubuntu 10.04 and CentOS 5.5
Posts: 3,873

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
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?

Last edited by stress_junkie; 12-18-2006 at 11:52 AM.
 
  


Reply

Tags
random number



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Random number generation jacques83 Programming 5 02-04-2006 12:38 PM
random number generation sailu_mvn Programming 4 03-10-2005 02:10 PM
Random number generators blufire Programming 5 11-09-2004 06:02 PM
Python Random number sethgeekx86 Programming 4 06-10-2004 08:55 AM
I need random number in C ... purpleburple Programming 4 10-28-2002 04:37 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:14 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration