Quote:
Originally Posted by vgersh99
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..
|