eb5ef4933046d771453efaaff32624bd5a794385
[BK-2020-03.git] / unitproc / bkt-randFloat
1 #!/bin/bash
2
3 #==BEGIN Function Definitions==
4 randFloat() {
5 # Desc: Output random float with no newline
6 # Usage: randFloat arg1
7 # Inputs: arg1: number of decimal places
8 # Output: stdout: float as decimal string
9 # Version: 0.0.1
10 # Note: Outputs float from 0.000... to 0.999...
11 # Note: Default number of decimals is 5.
12 # Ref/Attrib: Integer test regular expression https://stackoverflow.com/a/806923
13
14 # Validate input
15 if [[ $# -eq 0 ]]; then
16 : # do nothing
17 elif [[ $# -eq 1 ]]; then
18 arg1=$1;
19 RETEST1='^[0-9]+$'
20 if ! [[ $arg1 =~ $RETEST1 ]]; then
21 echo "ERROR:Not an integer." >&2; exit 1;
22 fi;
23 else
24 echo "ERROR:Invalid number of arguments:${@}";
25 fi;
26
27 # Produce output
28 decimals="$(head -c ${arg1:-5} < <(LC_ALL=C tr -cd "[:digit:]" < <(cat /dev/urandom)))";
29 echo 0."$decimals";
30 }
31 #==END Function Definitions==
32
33 #==BEGIN sample code==
34 time randFloat;
35 dec=5
36 time randFloat;
37 dec=70
38 time randFloat;
39 time (randFloat && randFloat && randFloat && randFloat && randFloat && randFloat && randFloat && randFloat && randFloat && randFloat);
40 time (randFloat "$dec" && randFloat "$dec" && randFloat "$dec" && randFloat "$dec" && randFloat "$dec" && randFloat "$dec" && randFloat "$dec" && randFloat "$dec" && randFloat "$dec" && randFloat "$dec");
41 time (randFloat "$dec" &>/dev/null && randFloat "$dec" &>/dev/null && randFloat "$dec" &>/dev/null && randFloat "$dec" &>/dev/null && randFloat "$dec" &>/dev/null && randFloat "$dec" &>/dev/null && randFloat "$dec" &>/dev/null && randFloat "$dec" &>/dev/null && randFloat "$dec" &>/dev/null && randFloat "$dec" &>/dev/null);
42 #==END sample code==