mod on %RANDOM | Unix Linux Forums | Shell Programming and Scripting

  Unix/Linux Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

mod on %RANDOM

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 12-28-2008
er_zeeshan05 er_zeeshan05 is offline
Registered User
 
Join Date: Oct 2008
Last Activity: 30 July 2011, 1:56 PM EDT
Posts: 41
Thanks: 1
Thanked 0 Times in 0 Posts
Hammer & Screwdriver mod on %RANDOM

I use %random to generate random number but i want to mod it so i get random number below 50 only in ksh script
how can i do it
x=random % 50
doesn't work

help is appreciated.


Is there any other way i get generate random number below 50 in ksh.any other function.
Plz provide an example

i am fairly new to unix
thanks
Sponsored Links
    #2  
Old Unix and Linux 12-28-2008
jpradley jpradley is offline
Registered User
 
Join Date: Apr 2008
Last Activity: 21 December 2013, 7:17 PM EST
Posts: 64
Thanks: 0
Thanked 0 Times in 0 Posts
try
x= `echo $RANDOM %50 | bc`
Sponsored Links
    #3  
Old Unix and Linux 12-28-2008
radoulov's Unix or Linux Image
radoulov radoulov is offline Forum Staff  
Moderator
 
Join Date: Jan 2007
Last Activity: 23 March 2015, 5:18 AM EDT
Location: Варна, България / Milano, Italia
Posts: 5,689
Thanks: 184
Thanked 623 Times in 581 Posts

Code:
$ m=50  
$ for i in {1..20};do print $(($RANDOM%$m+1));done      
4
31
11
4
50
16
12
6
44
39
40
26
23
42
41
8
44
20
47
36

    #4  
Old Unix and Linux 12-29-2008
Perderabo's Unix or Linux Image
Perderabo Perderabo is offline Forum Staff  
Unix Daemon (Administrator Emeritus)
 
Join Date: Aug 2001
Last Activity: 26 March 2015, 3:50 PM EDT
Location: Ashburn, Virginia
Posts: 9,931
Thanks: 64
Thanked 444 Times in 263 Posts
When you use a modulus operation you are selecting information from the low order bits of a number and discarding information from the high order bits.

"these bits should be extracted from the most significant (left-hand) part
of the computer word, since the least significant bits produced by many random number generators are not sufficiently random."

and

"The least significant (right-hand) digits of X are not very random, so decisions based on the number X should always be influenced primarily by the most significant digits. It is generally best to think of X as a random fraction X/m between 0 and 1, that is, to visualize X with a decimal point at its left, rather than to regard X as a random integer between 0 and m - 1. To compute a random integer between 0 and k - 1, one should multiply by k and truncate the result."
both from The Art of Computer Programming, Volume 2 Seminumerical Algorithms by Donald Knuth

While neither is perfect, assuming that 0 <= RANDOM <= 32767,
((myrandom = RANDOM * 50 / 32768))
will behave better than
((myrandom = RANDOM % 50))
Sponsored Links
    #5  
Old Unix and Linux 12-29-2008
radoulov's Unix or Linux Image
radoulov radoulov is offline Forum Staff  
Moderator
 
Join Date: Jan 2007
Last Activity: 23 March 2015, 5:18 AM EDT
Location: Варна, България / Milano, Italia
Posts: 5,689
Thanks: 184
Thanked 623 Times in 581 Posts
Thank you for elaborating further, Perderabo!
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Unix or Linux Image More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
$random relle Shell Programming and Scripting 6 07-11-2008 05:59 AM
Help with AWK and RANDOM please Kweekwom Shell Programming and Scripting 5 07-07-2008 01:57 PM
how to create random no between 10 to 40 in C useless79 Programming 5 08-19-2007 08:51 PM
Random keyvan Shell Programming and Scripting 8 05-24-2007 12:11 PM
random in ksh pascalbout AIX 1 01-04-2006 09:53 AM



All times are GMT -4. The time now is 04:01 PM.