+ Reply to Thread
Results 1 to 3 of 3

Thread: Bash Random Number

  1. #1
    imported_Oppo is offline Newbie
    Join Date
    Oct 2007
    Posts
    7
    Rep Power
    0

    Bash Random Number

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

    Code:
    NUMBER=`echo $RANDOM%900 | bc`
    Now you can use $NUMBER anywhere in your script and you should have a random number.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    asafe's Avatar
    asafe is offline Programmer
    Join Date
    Jul 2009
    Location
    Here
    Posts
    110
    Rep Power
    10

    Re: Bash Random Number

    If I'm not wrong RANDOM is a shell variable. I think you use it directily:

    Code:
    NUMBER=$RANDOM
    Or
    Code:
    number=$((RANDOM % somenumber))
    Last edited by asafe; 08-03-2010 at 11:35 AM.

  4. #3
    Exit526 is offline Newbie
    Join Date
    Apr 2010
    Posts
    1
    Rep Power
    0

    Re: Bash Random Number

    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.
    Code:
    cat /dev/urandom|od -N4 -An -i
    See http://94.228.168.188/en/articles/article_03.html for details.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Random number generator?
    By sjshacker93 in forum Visual Basic Programming
    Replies: 5
    Last Post: 09-03-2010, 09:27 PM
  2. Random Number Help
    By Alex_j in forum C# Programming
    Replies: 3
    Last Post: 11-13-2009, 08:01 AM
  3. Bash: Getting Number/Count of Files in Directory
    By Tor in forum Bash / Shell Scripting
    Replies: 0
    Last Post: 02-12-2009, 10:59 AM
  4. Random number gen question
    By nemr118 in forum Visual Basic Programming
    Replies: 8
    Last Post: 02-06-2009, 08:29 AM
  5. to not make a random number appear twice
    By Darkyere in forum Pascal and Delphi
    Replies: 2
    Last Post: 12-03-2008, 08:30 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts