From: Steven Baltakatei Sandoval Date: Wed, 4 Aug 2021 11:35:48 +0000 (+0000) Subject: feat(unitproc/python):Add sleepRand.py X-Git-Tag: 0.5.0~50 X-Git-Url: https://zdv2.bktei.com/gitweb/BK-2020-03.git/commitdiff_plain/ae63d7db7c6cf2e015e0a59645031d7ddac817a6?hp=-c feat(unitproc/python):Add sleepRand.py - note: sleeps for random amount of time using inverse gaussian distribution. --- ae63d7db7c6cf2e015e0a59645031d7ddac817a6 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)); + +