LinuxQuestions.org
Help answer threads with 0 replies.
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 10-13-2015, 07:00 AM   #1
emlynjose
LQ Newbie
 
Registered: Sep 2015
Posts: 7

Rep: Reputation: Disabled
shell script to display a random number from a list


hi,

I need to run a main script on a number of servers after sleeping for a random seconds. My plan is to add this random-number-selection-shell-script at the beginning of this main script.
Below is the idea:

random_list={0,1800,3600, 5400,....,86400}
sleep $random_list
..........
main script follows..
..........

Here $random_list should take a random value from the above list. Please help on this!!

Regards,
Emlyn Jose
 
Old 10-13-2015, 07:42 AM   #2
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
Hi

You could use a bash array and pull a random item from the array.

Code:
list=(0 1800 3600 5400 86400)
echo ${list[$RANDOM % ${#list[@]} ]}
 
Old 10-13-2015, 03:37 PM   #3
Rinndalir
Member
 
Registered: Sep 2015
Posts: 733

Rep: Reputation: Disabled
Do you have a range?

perl -e 'print int rand(86400)'

or

od -d -N 2 /dev/urandom| awk '{print $2}'
 
Old 10-13-2015, 10:31 PM   #4
emlynjose
LQ Newbie
 
Registered: Sep 2015
Posts: 7

Original Poster
Rep: Reputation: Disabled
Thank you for the codes above.
@Rinndalir: This works but I need values from the list of given values with 1800 as difference.
@Guttorm: This really works. But I am planning to give a range from 0 to 86400 with 1800 as difference. Can you please help me with that?
i.e., list = { 0 - 86400 }
so that $list will have values like 0,1800,3600, 5400,7200, ......., 86400.

Also it will be helpful if you explain the snippet. Like, i did not understand what echo ${list[$RANDOM ${#list[@]} ]} does But it does give the result.

BR,Emlyn Jose
 
Old 10-13-2015, 10:55 PM   #5
Sefyir
Member
 
Registered: Mar 2015
Distribution: Linux Mint
Posts: 634

Rep: Reputation: 316Reputation: 316Reputation: 316Reputation: 316
You can't have incremental ranges (that I'm aware of) using {}
But you could always do this:

Code:
list=$(for i in {1..48}; do echo -n "$((1800 * $i)) "; done)
Code:
echo $list
1800 3600 5400 7200 9000 10800 12600 14400 16200 18000 19800 21600 23400 25200 27000 28800 30600 32400 34200 36000 37800 39600 41400 43200 45000 46800 48600 50400 52200 54000 55800 57600 59400 61200 63000 64800 66600 68400 70200 72000 73800 75600 77400 79200 81000 82800 84600 86400
 
Old 10-13-2015, 11:22 PM   #6
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,702

Rep: Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896
$RANDOM will produce a random number from 0-32767. In a nutshell to create a certain range you use the modulo function and so the equation below will generate numbers from 0 to X-1.

r=$(( $RANDOM % X ))

Guttorm's code generates a random number in the same manner using the number of elements in the array as X. So ${list[$RANDOM % ${#list[@]} ]} actually in the posted example is ${list[$RANDOM % 5]}. So the values will be list[0]=0, list[1]=1800 ... list[4]=86400.

As Sefyir posted you can create an array in a similar manner but is not required.


r=$(( $RANDOM%49*1800 ))

Last edited by michaelk; 10-13-2015 at 11:53 PM.
 
  


Reply



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
How to write a script to pick a random number 1-10 to generate a winner JDC10 Programming 11 03-29-2012 06:55 AM
looking for a program to display random words from list newbiesforever Linux - Software 10 04-23-2011 01:21 PM
LXer: Brain Teaser: Seemingly Random Number List Selection LXer Syndicated Linux News 0 09-10-2008 11:41 AM
Generate a random number from a bourne shell script lothario Linux - Software 2 03-01-2007 11:01 PM

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

All times are GMT -5. The time now is 10:43 AM.

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