Help answer threads with 0 replies.
Go Back > Forums > Linux Forums > Linux - General
User Name
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.


  Search this Thread
Old 07-13-2004, 11:56 AM   #1
Registered: Sep 2001
Distribution: Ubuntu, Debian, Gentoo
Posts: 162

Rep: Reputation: 30
Creating random numbers from shell with /dev/random

Could someone point me in the right direction for creating random rumbers within a specific range with /dev/random or /dev/urandom? Say that I want to generate numbers between 1 and 5000, how would I do that? Or maybe even between -35 and 79? Thanks in advance...

Kristian Hermansen
Old 07-13-2004, 01:12 PM   #2
Senior Member
Registered: Feb 2003
Location: N'rn WI -- USA
Distribution: Kubuntu 8.04, ClarkConnect 4
Posts: 1,142

Rep: Reputation: 47
Bash has a "magic" variable called RANDOM. "echo $RANDOM" will give you a random number. I don't know the max value off hand, but it usually returns a number in the tens-of-thousands.

You can set a max value with "$[ ( $RANDOM % max_val ) ]". So, "echo $[ ( $RANDOM % 5000 ) ]" should be [0,5000]. "echo $[ ( $RANDOM % 4999 + 1 ) ]" should be [1,5000]. "echo $[ ( $RANDOM % 114 - 35 ) ]" should be [-35,79].

You can seed RANDOM with "RANDOM=some_value". Maybe use date in seconds... "RANDOM=$(date +%s)".

Search Google for more info.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
generating random numbers in C abk4523 Programming 20 01-10-2011 05:08 PM
Random numbers in C loke137 Programming 9 09-11-2010 11:37 AM
Shell scripting - Random numbers within a range felixc Linux - Newbie 2 10-09-2005 06:41 PM
random numbers deveraux83 Programming 4 05-17-2005 03:26 AM
random numbers deveraux83 Programming 2 02-28-2005 06:36 PM

All times are GMT -5. The time now is 05:42 PM.

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