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?

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 ?

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

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:

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

OR

nawk 'BEGIN {max=10000;srand(); printf("%.2f\n", ((int(max*rand())%11)+5)/10)}'
[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

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

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

[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..

As pludi mentioned in the second post on this thread - you have to do it yourself.
As an alternative (which might be 'good enough' - but I doubt it with 1ms frequency), use the Search function of these forums to see other threads addressing 'randomness'.

All in all, a 1ms frequency I'd say is a bit too much for the scripting paradigm.

How come you didn't say that in your first post? Nobody here can read your mind. If you want unique random numbers then that is a different situation that just random numbers. And since the amount of numbers between .5 and 1.5 is very limited you should say how many random numbers you want, otherwise all you have to do is shuffle the list of numbers in a random way and not create a list of random numbers.

Edit: never mind, I didn't read your last post that showed your usage requirements. Shuffling a list of numbers will not work for your requirements.

Hi.

Here is one method:

#!/usr/bin/env bash

# @(#) s1       Demonstrate obtaining vector of truly random values.

echo
set +o nounset
LC_ALL=C ; LANG=C ; export LC_ALL LANG
echo "Environment: LC_ALL = $LC_ALL, LANG = $LANG"
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version "=o" $(_eat $0 $1) awk qrandom
set -o nounset

echo
echo " Results, 0-1:"
qrandom 10

echo
echo " Results, 0.5 - 1.5:"
qrandom 10 |
awk '
{ t = $0 + 0.5 ; printf("%.2f\n",t) }
'

exit 0

Producing a sequence (0,1) and (.5,1.5):

% ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.11-x1, i686
Distribution        : Xandros Desktop 3.0.3 Business
GNU bash 2.05b.0
GNU Awk 3.1.4
qrandom (local) 1.0

 Results, 0-1:
0.4673012
0.7736701
0.9535608
0.1947628
0.8967344
0.4138898
0.9485450
0.6075562
0.9707173
0.2477338

 Results, 0.5 - 1.5:
1.27
0.57
1.00
1.01
1.26
1.47
0.63
0.84
0.92
0.91

The random number vector is obtained from an Internet service:

Best wishes ... cheers, drl

ksh93 does floating point arithmetic:

$ for n in 1 2 3 4 5 
> do
>  echo $(( ($RANDOM % 11 + 5) / 10. ))
> done
0.6
0.5
1.2
1.1
1.4