Hi All,
I have extracted some report from database for few activities done.
Now I have a requirement to add some random time(In range of 10-35) in front of each activity.
Can be generated random numbers in any bash/sh shell within a given number range, let's say in between 10-30.
Could you please help me.
panyam
July 22, 2011, 10:03am
2
Have a look at
$RANDOM
Manipulate it a bit to get numbers b/w 10 and 30
Panyam, thanks for revert, but how do I manipulate it ? any suggestion ?
panyam
July 22, 2011, 10:39am
4
echo "10+$RANDOM%10*(2)+2 " | bc
will generate random numbers in 12~30 range, if that suffice.
1 Like
Thanks Panyam for your useful & quick help, this is what I wanted to do.
kshji
July 23, 2011, 5:46am
6
You can use also builtin calculation, tested using ksh and bash
echo $(( 10+RANDOM%21 ))
# or more generic
min=10
max=30
((diff=max-min+1))
echo $(( min + RANDOM%diff ))
1 Like
drl
July 23, 2011, 11:03am
7
Hi.
shuf -i 10-30 -n 1
produces, in 3 separate runs:
26
% shuf -i 10-30 -n 1
15
% shuf -i 10-30 -n 1
25
The shuf utility is part of GNU coreutils, Coreutils - GNU core utilities which you could download, configure, compile, etc., if your system does not have coreutils installed or available via your package manager -- easier for Linux than for one of the Unix systems, Solaris, *BSD, etc.
Many features, see on-line man page is at: shuf(1): make random permutations - Linux man page
Best wishes ... cheers, drl
For the heck of it, using sh and /dev/random, or when shell does not have $RANDOM
#!/bin/sh
if [ -z "$1" ]; then a=0; b=65536; else b=$((1+$1)); fi
[ -z "$2" ] || { a="$1"; b=$((1+$2-a));}
echo $((a+$(od -An -dN2 /dev/random)%b))
exit 0
Usage: this_script.sh
outputs a value between 0 and 65535
this_script.sh MAX
outputs a value between 0 and MAX
this_script.sh MIN MAX
outputs a value between MIN and MAX
drl
July 24, 2011, 8:06am
9
Hi.
Another handy utility, jot produces, for 3 runs:
% jot -r 1 10 30
29
% jot -r 1 10 30
10
% jot -r 1 10 30
12
I first ran across jot in UNIX PowerTools, Second Edition - O'Reilly Media , but the version on Debian 5 (lenny) has been extended beyond that. There is a source tar file at http://ftp.de.debian.org/debian/pool/main/a/athena-jot/athena-jot_9.0.orig.tar.gz -- I unpacked, configured, and compiled the source successfully:
% ./jot -r 1 100 500
346
Like shuf, jot has more features, see Man Page for jot (FreeBSD Section 1) - The UNIX and Linux Forums
Best wishes ... cheers, drl
Hey...Thanks to all for providing such useful info.
drl
July 25, 2011, 9:59am
11
Hi.
There is a characteristic of /dev/random that could become an issue. There may be a limit to how many bits one can pull from /dev/random because it may block. The alternate /dev/urandom is not blocked and is probably as good as /dev/random for many purposes.
For example, I can only get 200-300 numbers from the posted script before /dev/random blocks (Debian 5 -- lenny -- GNU/Linux).
See /dev/random - Wikipedia, the free encyclopedia for details on this and other aspects ... cheers, drl