The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Get Data Between a specific Date Range from logs sankasu Shell Programming and Scripting 4 06-11-2009 12:30 AM
Match a specific IP range sylaan Shell Programming and Scripting 5 07-16-2008 07:42 AM
Generating line number Amey Joshi UNIX for Dummies Questions & Answers 5 07-09-2008 06:12 AM
generating random numbers with hamming distance 4 hack_tom Shell Programming and Scripting 2 04-29-2007 02:27 AM
Generating files of specific size nxd25 Shell Programming and Scripting 2 06-27-2006 12:06 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-03-2009
TehOne TehOne is offline
Registered User
  
 

Join Date: Oct 2008
Posts: 55
Generating random number within a specific range (0.5-1.5)

Hello, need a way to generate numbers within 0.5-1.5 range

Has to be totally random:

0.6
1.1
0.8
1.5
0.6
and so on....


How to?
  #2 (permalink)  
Old 04-03-2009
pludi's Avatar
pludi pludi is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2008
Location: .at
Posts: 1,949
Take ${RANDOM}, mod by 11, add 5, divide by 10. If that's not random enough, implement yourself a Blum-Blum-Shub-PRNG
  #3 (permalink)  
Old 04-03-2009
TehOne TehOne is offline
Registered User
  
 

Join Date: Oct 2008
Posts: 55
Quote:
Originally Posted by pludi View Post
Take ${RANDOM}, mod by 11, add 5, divide by 10. If that's not random enough, implement yourself a Blum-Blum-Shub-PRNG
and how will that stay within 0.5 and 1.5 ?
  #4 (permalink)  
Old 04-03-2009
pludi's Avatar
pludi pludi is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2008
Location: .at
Posts: 1,949
The RANDOM variable will generate a random integer every time it's read, say 7081 or 17285. The modulo operation will return the remainder of a division, which can only be between 0 and divisor-1, so with 11 it's between 0 and 10. Add 5 so it's between 5 and 15. Divide by 10, and it's between .5 and 1.5

7081 % 11 = 8
8 + 5 = 13
13 / 10 = 1.3

17285 % 11 = 4
4 + 5 = 9
9 / 10 = 0.9
  #5 (permalink)  
Old 04-03-2009
TehOne TehOne is offline
Registered User
  
 

Join Date: Oct 2008
Posts: 55
echo $((((RANDOM%11)+5)/10))
1
echo $((((RANDOM%11)+5)/10))
0

That's not what I need.... look my first post I gave some output examples
  #6 (permalink)  
Old 04-03-2009
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,131
Quote:
Originally Posted by TehOne View Post
echo $((((RANDOM%11)+5)/10))
1
echo $((((RANDOM%11)+5)/10))
0

That's not what I need.... look my first post I gave some output examples
All ksh arithmetic is integer.
Use 'bc' for the floating point arithmetic.
something along the lines:

Code:
echo "scale=2;(((${RANDOM} % 11)+5)/10)" |bc

OR

Code:
nawk 'BEGIN {max=10000;srand(); printf("%.2f\n", ((int(max*rand())%11)+5)/10)}'


Last edited by vgersh99; 04-03-2009 at 01:42 PM..
  #7 (permalink)  
Old 04-03-2009
TehOne TehOne is offline
Registered User
  
 

Join Date: Oct 2008
Posts: 55
Quote:
Originally Posted by vgersh99 View Post
All ksh arithmetic is integer.
Use 'bc' for the floating point arithmetic.
something along the lines:

Code:
echo "scale=2;(((${RANDOM} % 11)+5)/10)" |bc

OR

Code:
nawk 'BEGIN {max=10000;srand(); printf("%.2f\n", ((int(max*rand())%11)+5)/10)}'

Code:
[19:02:20] root:~# echo "scale=2;(((${RANDOM} % 11)+5)/10)" | bc
.50
[19:02:20] root:~# echo "scale=2;(((${RANDOM} % 11)+5)/10)" | bc
.51
[19:02:21] root:~# echo "scale=2;(((${RANDOM} % 11)+5)/10)" | bc
.50
[19:02:21] root:~# echo "scale=2;(((${RANDOM} % 11)+5)/10)" | bc
.50
[19:02:21] root:~# echo "scale=2;(((${RANDOM} % 11)+5)/10)" | bc
.50

It only gives .50 or .51 as output...

and when using quickly


Code:
nawk 'BEGIN {max=10000;srand(); printf("%.2f\n", ((int(max*rand())%11)+5)/10)}'

it gives a couple times the same result... example:


Code:
[19:03:17] root:~# nawk 'BEGIN {max=10000;srand(); printf("%.2f\n", ((int(max*rand())%11)+5)/10)}'
0.60
[19:03:18] root:~# nawk 'BEGIN {max=10000;srand(); printf("%.2f\n", ((int(max*rand())%11)+5)/10)}'
0.60
[19:03:18] root:~# nawk 'BEGIN {max=10000;srand(); printf("%.2f\n", ((int(max*rand())%11)+5)/10)}'
0.60
[19:03:18] root:~# nawk 'BEGIN {max=10000;srand(); printf("%.2f\n", ((int(max*rand())%11)+5)/10)}'
0.60
[19:03:18] root:~# nawk 'BEGIN {max=10000;srand(); printf("%.2f\n", ((int(max*rand())%11)+5)/10)}'
0.60
[19:03:18] root:~# nawk 'BEGIN {max=10000;srand(); printf("%.2f\n", ((int(max*rand())%11)+5)/10)}'
0.60
[19:03:18] root:~# nawk 'BEGIN {max=10000;srand(); printf("%.2f\n", ((int(max*rand())%11)+5)/10)}'
1.00
[19:03:19] root:~# nawk 'BEGIN {max=10000;srand(); printf("%.2f\n", ((int(max*rand())%11)+5)/10)}'
1.00
[19:03:19] root:~# nawk 'BEGIN {max=10000;srand(); printf("%.2f\n", ((int(max*rand())%11)+5)/10)}'
1.00
[19:03:19] root:~# nawk 'BEGIN {max=10000;srand(); printf("%.2f\n", ((int(max*rand())%11)+5)/10)}'
1.00

My script will be using the random command sometimes a few times within one second and having the same result each time is not an option..

Last edited by TehOne; 04-03-2009 at 02:12 PM..
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:25 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0