From: Steven Baltakatei Sandoval Date: Wed, 4 Aug 2021 11:36:26 +0000 (+0000) Subject: Merge branch 'develop' of https://zdv2.bktei.com/gitweb/baltakatei-exdev into develop X-Git-Tag: 0.5.0~49 X-Git-Url: https://zdv2.bktei.com/gitweb/BK-2020-03.git/commitdiff_plain/9c0c61ff32b0f0594cdb70a0aeacb190caf6022b?hp=2e5e9a712d8baa108ba6ef680343a386138e2c80 Merge branch 'develop' of https://zdv2.bktei.com/gitweb/baltakatei-exdev into develop --- diff --git a/unitproc/python/sleepRand.py b/unitproc/python/sleepRand.py new file mode 100755 index 0000000..76a32e1 --- /dev/null +++ b/unitproc/python/sleepRand.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python3 +# Desc: Pauses a random amount of time. Random distribution is inverse gaussian. +# Version: 0.0.1 +# Depends: python 3.7.3 +# Usage: ./sleepRand.py arg1 +# Input: arg1: float seconds (mean of inverse gaussian distribution) +# Example: python3 sleepRand.py 4.0 + +import math, time, random, sys + +# Define functions +def randInvGau(mu, lam): + """Returns random variate of inverse gaussian distribution""" + # Ref/Attrib: doi:10.1080/00031305.1976.10479147 + nu = random.gauss(0,1); + y = nu ** 2; + xTerm1 = mu; + xTerm2 = mu ** 2 * y / (2 * lam); + xTerm3 = (- mu / (2 * lam)) * math.sqrt(4 * mu * lam * y + mu ** 2 * y ** 2); + x = xTerm1 + xTerm2 + xTerm3; + z = random.uniform(0.0,1.0); + if z <= (mu / (mu + x)): + return x; + else: + return (mu ** 2 / x); + +# Check input (TODO) +arg1 = float(sys.argv[1]); # first argument +desMean = arg1; + +# Configure +lambdaFactor = 4; # spread factor; inversely proportional to variance + +# Calculate delay +delay = randInvGau(desMean, desMean * lambdaFactor); +#print('DEBUG:delay:' + str(float(delay))); + +# Sleep +time.sleep(float(delay)); + +