Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Random numbers in shell (korn) scripts.

Status
Not open for further replies.

mckarl

Programmer
May 5, 1999
147
GB
hi all, well... i need a random number in my shell script.<br>- and the best way i have found is:<br><br>------------------------------------------------------------<br><br>echo &quot;Three numbers will now be chosen at random.&quot;<br><br>ran1=$RANDOM<br>ran2=$RANDOM<br>ran3=$RANDOM<br><br>size1=${#ran1}00<br>size2=${#ran2}00<br>size3=${#ran3}00<br><br>random1=`expr $ran1 / $size1`<br>random2=`expr $ran2 / $size2`<br>random3=`expr $ran3 / $size3`<br><br>echo &quot;1. $random1&quot;<br>echo &quot;2. $random2&quot;<br>echo &quot;3. $random3&quot;<br><br>------------------------------------------------------------<br><br>Please tell me there is an easier/better way to do this!!&nbsp;&nbsp;<br><br>Thankyou in advance, Karl.<br> <p> Karl<br><a href=mailto:mc_karl@yahoo.com>mc_karl@yahoo.com</a><br><a href= > </a><br> ~ ~ ~ ~<br>
K A R L<br>
~ ~ ~ ~
 
It´s a good way, but try multiplying or dividing a pair of random numbers... it´s better. Or you can use SECONDS var too.<br><br>I hope it works...
 
What i hope to do, is specify a range of numbers&nbsp;&nbsp;( ie 1-10)<br><br>-- so i can generate a number between those numbers.<br><br>there is the RANDOM<i>=10</i> thingy, but i dont really know how that works... <br><br>-- Please can someone tell me the answer??<br><br>---- Thanks in advance, karl! <p> Karl<br><a href=mailto:mc_karl@yahoo.com>mc_karl@yahoo.com</a><br><a href= > </a><br> ~ ~ ~ ~<br>
K A R L<br>
~ ~ ~ ~
 
I guess is easier to divide thr RANDOM number by 3277 and add 1. That willgive you numbers between 1 and 10<br><br>Ihope it works..
 
cheers ramon!!<br>-- like a dream.......<br><br>Karl. <p> Karl<br><a href=mailto:mc_karl@yahoo.com>mc_karl@yahoo.com</a><br><a href= > </a><br> ~ ~ ~ ~<br>
K A R L<br>
~ ~ ~ ~
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top