chore(unitproc/bkt-randFloat):Add more sample code
[BK-2020-03.git] / unitproc / bkt-randFloat
CommitLineData
891bb4bd
SBS
1#!/bin/bash
2
3#==BEGIN Function Definitions==
4randFloat() {
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==
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==