Improve PoorMansRandomGenerator function
This commit is contained in:
parent
dde31090fe
commit
a230d32042
|
@ -8,7 +8,7 @@
|
||||||
#### OFUNCTIONS FULL SUBSET ####
|
#### OFUNCTIONS FULL SUBSET ####
|
||||||
#### OFUNCTIONS MINI SUBSET ####
|
#### OFUNCTIONS MINI SUBSET ####
|
||||||
_OFUNCTIONS_VERSION=2.3.0-RC1
|
_OFUNCTIONS_VERSION=2.3.0-RC1
|
||||||
_OFUNCTIONS_BUILD=2018100101
|
_OFUNCTIONS_BUILD=2018100102
|
||||||
#### _OFUNCTIONS_BOOTSTRAP SUBSET ####
|
#### _OFUNCTIONS_BOOTSTRAP SUBSET ####
|
||||||
_OFUNCTIONS_BOOTSTRAP=true
|
_OFUNCTIONS_BOOTSTRAP=true
|
||||||
#### _OFUNCTIONS_BOOTSTRAP SUBSET END ####
|
#### _OFUNCTIONS_BOOTSTRAP SUBSET END ####
|
||||||
|
@ -106,37 +106,16 @@ fi
|
||||||
# Get a random number on Windows BusyBox alike, also works on most Unixes
|
# Get a random number on Windows BusyBox alike, also works on most Unixes
|
||||||
function PoorMansRandomGenerator {
|
function PoorMansRandomGenerator {
|
||||||
local digits="${1}" # The number of digits to generate
|
local digits="${1}" # The number of digits to generate
|
||||||
|
local number
|
||||||
|
|
||||||
local minimum=1
|
# Some read bytes can't be used, se we read twice the number of required bytes
|
||||||
local maximum
|
dd if=/dev/urandom bs=$digits count=2 | while read -r -n1 char; do
|
||||||
local n=0
|
number=$number$(printf "%d" "'$char")
|
||||||
|
if [ ${#number} -ge $digits ]; then
|
||||||
if [ "$digits" == "" ]; then
|
echo ${number:0:$digits}
|
||||||
digits=5
|
break;
|
||||||
fi
|
|
||||||
|
|
||||||
# Minimum already has a digit
|
|
||||||
for n in $(seq 1 $((digits-1))); do
|
|
||||||
minimum=$minimum"0"
|
|
||||||
maximum=$maximum"9"
|
|
||||||
done
|
|
||||||
maximum=$maximum"9"
|
|
||||||
|
|
||||||
#n=0; while [ $n -lt $minimum ]; do n=$n$(dd if=/dev/urandom bs=100 count=1 2>/dev/null | tr -cd '0-9'); done; n=$(echo $n | sed -e 's/^0//')
|
|
||||||
# bs=19 since if real random strikes, having a 19 digits number is not supported
|
|
||||||
while [ $n -lt $minimum ] || [ $n -gt $maximum ]; do
|
|
||||||
if [ $n -lt $minimum ]; then
|
|
||||||
# Add numbers
|
|
||||||
n=$n$(dd if=/dev/urandom bs=19 count=1 2>/dev/null | tr -cd '0-9')
|
|
||||||
n=$(echo $n | sed -e 's/^0//')
|
|
||||||
if [ "$n" == "" ]; then
|
|
||||||
n=0
|
|
||||||
fi
|
|
||||||
elif [ $n -gt $maximum ]; then
|
|
||||||
n=$(echo $n | sed 's/.$//')
|
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
echo $n
|
|
||||||
}
|
}
|
||||||
#### PoorMansRandomGenerator SUBSET END ####
|
#### PoorMansRandomGenerator SUBSET END ####
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue