rand: --seed implemented.
This commit is contained in:
parent
0047319b1b
commit
60744cc445
36
rand/rand
36
rand/rand
|
@ -10,7 +10,7 @@ rand - generate (pseudo-)random data
|
|||
|
||||
=head1 SYNOPSIS
|
||||
|
||||
B<rand> [-f FORMAT|--format FORMAT] [[I<from>] I<to>]
|
||||
B<rand> [-f FORMAT|--format FORMAT] [-s SEED|--seed SEED] [[I<from>] I<to>]
|
||||
|
||||
|
||||
=head1 DESCRIPTION
|
||||
|
@ -37,6 +37,13 @@ If I<from> is also given, numbers will be integers from I<from> to I<to>.
|
|||
|
||||
Format number using I<FORMAT>. I<FORMAT> follows B<printf>s specification.
|
||||
|
||||
|
||||
=item B<--seed> I<SEED>
|
||||
|
||||
=item B<-s> I<SEED>
|
||||
|
||||
Use SEED for random number generator for reproducible numbers.
|
||||
|
||||
=back
|
||||
|
||||
|
||||
|
@ -98,10 +105,16 @@ B<openssl>
|
|||
=cut
|
||||
|
||||
randdata() {
|
||||
# Generate random 8-bit data by AES encrypting /dev/zero with a
|
||||
# random key
|
||||
# Generate random 8-bit data by AES encrypting /dev/zero
|
||||
if [ "$seed" == "" ] ; then
|
||||
# use a random key
|
||||
key=$(openssl rand -hex 16)
|
||||
iv=$(openssl rand -hex 16)
|
||||
else
|
||||
# use seed from --seed
|
||||
key=$(echo "$seed" | openssl sha256 -hex | cut -d' ' -f2)
|
||||
iv=$(echo "$seed" | openssl sha512 -hex | cut -d' ' -f2)
|
||||
fi
|
||||
< /dev/zero openssl enc -aes-128-ctr -K $key -iv $iv 2>/dev/null
|
||||
}
|
||||
export -f randdata
|
||||
|
@ -131,6 +144,7 @@ fmt='%s\n'
|
|||
while true; do
|
||||
case "$1" in
|
||||
-f | --fmt ) fmt="$2"; shift 2 ;;
|
||||
-s | --seed ) seed="$2"; shift 2 ;;
|
||||
-D | --debug ) debug=true; shift ;;
|
||||
-h | --help ) help=1; shift 2 ;;
|
||||
-- ) shift; break ;;
|
||||
|
@ -139,6 +153,7 @@ while true; do
|
|||
done
|
||||
qfmt="$(parallel --shellquote --shellquote ::: "$fmt")"
|
||||
|
||||
if [ "$seed" == "" ] ; then
|
||||
if [ "$1" == "" ] ; then
|
||||
# Boost performance by running 1 per CPU core
|
||||
eval parallel -u randdata ::: {1..$(parallel --number-of-threads)}
|
||||
|
@ -152,3 +167,18 @@ else
|
|||
eval parallel -N0 --lb randints $1 $2 "$qfmt" ::: {1..$(parallel --number-of-threads)}
|
||||
fi
|
||||
fi
|
||||
else
|
||||
if [ "$1" == "" ] ; then
|
||||
# Run a single instance
|
||||
randdata
|
||||
else
|
||||
# $1 set => numerical
|
||||
if [ "$2" == "" ] ; then
|
||||
# 0 .. $1
|
||||
randints 0 $1 "$fmt"
|
||||
else
|
||||
# $1 .. $2
|
||||
randints $1 $2 "$fmt"
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
|
|
Loading…
Reference in a new issue