Jump to content

Bash Random Number

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
imported_Oppo

imported_Oppo

    Newbie

  • Members
  • Pip
  • 7 posts
I thought I would post how to generate a random number using Bash because it took me a while to find the information.

NUMBER=`echo $RANDOM%900 | bc`

Now you can use $NUMBER anywhere in your script and you should have a random number.

#2
asafe

asafe

    Programmer

  • Members
  • PipPipPipPip
  • 107 posts
If I'm not wrong RANDOM is a shell variable. I think you use it directily:

NUMBER=$RANDOM  

Or
number=$((RANDOM % somenumber)) 

Edited by asafe, 03 August 2010 - 10:35 AM.


#3
Exit526

Exit526

    Newbie

  • Members
  • Pip
  • 1 posts
I think the better way is to use /dev/urandom for this. Read random bits from the file. You can obtain large range. $RANDOM has only 0-32767. E.g.
cat /dev/urandom|od -N4 -An -i

See http://94.228.168.18...article_03.html for details.