From: Steven Baltakatei Sandoval Date: Wed, 28 Jul 2021 02:30:59 +0000 (+0000) Subject: feat(unitproc/bktemp-randFloat):random float string generator X-Git-Tag: 0.5.0~52 X-Git-Url: https://zdv2.bktei.com/gitweb/BK-2020-03.git/commitdiff_plain/891bb4bd2a14c4c91f94de6cd17b944a12e87194?hp=-c feat(unitproc/bktemp-randFloat):random float string generator --- 891bb4bd2a14c4c91f94de6cd17b944a12e87194 diff --git a/unitproc/bktemp-randFloat b/unitproc/bktemp-randFloat new file mode 100644 index 0000000..138653f --- /dev/null +++ b/unitproc/bktemp-randFloat @@ -0,0 +1,39 @@ +#!/bin/bash + +#==BEGIN Function Definitions== +randFloat() { + # Desc: Output random float with no newline + # Usage: randFloat arg1 + # Inputs: arg1: number of decimal places + # Output: stdout: float as decimal string + # Version: 0.0.1 + # Note: Outputs float from 0.000... to 0.999... + # Note: Default number of decimals is 5. + # Ref/Attrib: Integer test regular expression https://stackoverflow.com/a/806923 + + # Validate input + if [[ $# -eq 0 ]]; then + : # do nothing + elif [[ $# -eq 1 ]]; then + arg1=$1; + RETEST1='^[0-9]+$' + if ! [[ $arg1 =~ $RETEST1 ]]; then + echo "ERROR:Not an integer." >&2; exit 1; + fi; + else + echo "ERROR:Invalid number of arguments:${@}"; + fi; + + # Produce output + decimals="$(head -c ${arg1:-5} < <(LC_ALL=C tr -cd "[:digit:]" < <(cat /dev/urandom)))"; + echo 0."$decimals"; +} +#==END Function Definitions== + +#==BEGIN sample code== +dec=70 +time randFloat; +time (randFloat && randFloat && randFloat && randFloat && randFloat && randFloat && randFloat && randFloat && randFloat && randFloat); +time (randFloat "$dec" && randFloat "$dec" && randFloat "$dec" && randFloat "$dec" && randFloat "$dec" && randFloat "$dec" && randFloat "$dec" && randFloat "$dec" && randFloat "$dec" && randFloat "$dec"); +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); +#==END sample code==