feat(unitproc/bkt-replace_apos) Handle more replacement cases
[BK-2020-03.git] / unitproc / bkt-randFloat
CommitLineData
891bb4bd
SBS
1#!/bin/bash
2
3#==BEGIN Function Definitions==
4randFloat() {
9ed7bc1b 5 # Desc: Output random float
891bb4bd
SBS
6 # Usage: randFloat arg1
7 # Inputs: arg1: number of decimal places
8 # Output: stdout: float as decimal string
7367ee42 9 # Version: 0.1.1
891bb4bd
SBS
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
7367ee42 24 echo "ERROR:Invalid number of arguments:${*}";
891bb4bd
SBS
25 fi;
26
27 # Produce output
7367ee42 28 decimals="$(head -c "${arg1:-5}" < <(LC_ALL=C tr -cd "[:digit:]" < <(cat /dev/urandom)))";
9ed7bc1b
SBS
29 printf "0.%s\n" "$decimals";
30}; # output random float [0.00000 1.00000] to stdout
891bb4bd
SBS
31#==END Function Definitions==
32
33#==BEGIN sample code==
577de25d
SBS
34time randFloat;
35dec=5
36time randFloat;
891bb4bd
SBS
37dec=70
38time randFloat;
39time (randFloat && randFloat && randFloat && randFloat && randFloat && randFloat && randFloat && randFloat && randFloat && randFloat);
40time (randFloat "$dec" && randFloat "$dec" && randFloat "$dec" && randFloat "$dec" && randFloat "$dec" && randFloat "$dec" && randFloat "$dec" && randFloat "$dec" && randFloat "$dec" && randFloat "$dec");
41time (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==