
Forums  Search Forums  Register  Forum Rules  Man Pages  Albums  FAQ  Members  Calendar  Search  Today's Posts  Mark Forums Read 
Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. 

Thread Tools  Search this Thread  Display Modes 
#1




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




try
x= `echo $RANDOM %50  bc` 
Sponsored Links  

#3




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




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 (lefthand) part of the computer word, since the least significant bits produced by many random number generators are not sufficiently random." and "The least significant (righthand) 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




Thank you for elaborating further, Perderabo!

Sponsored Links  

Thread Tools  Search this Thread 
Display Modes  
More UNIX and Linux Forum Topics You Might Find Helpful  
Thread  Thread Starter  Forum  Replies  Last Post 
$random  relle  Shell Programming and Scripting  6  07112008 06:59 AM 
Help with AWK and RANDOM please  Kweekwom  Shell Programming and Scripting  5  07072008 02:57 PM 
how to create random no between 10 to 40 in C  useless79  Programming  5  08192007 09:51 PM 
Random  keyvan  Shell Programming and Scripting  8  05242007 01:11 PM 
random in ksh  pascalbout  AIX  1  01042006 10:53 AM 
