76a32e10172aa696e911e11a068817a3fe9d6c33
[BK-2020-03.git] / unitproc / python / sleepRand.py
1 #!/usr/bin/env python3
2 # Desc: Pauses a random amount of time. Random distribution is inverse gaussian.
3 # Version: 0.0.1
4 # Depends: python 3.7.3
5 # Usage: ./sleepRand.py arg1
6 # Input: arg1: float seconds (mean of inverse gaussian distribution)
7 # Example: python3 sleepRand.py 4.0
8
9 import math, time, random, sys
10
11 # Define functions
12 def randInvGau(mu, lam):
13 """Returns random variate of inverse gaussian distribution"""
14 # Ref/Attrib: doi:10.1080/00031305.1976.10479147
15 nu = random.gauss(0,1);
16 y = nu ** 2;
17 xTerm1 = mu;
18 xTerm2 = mu ** 2 * y / (2 * lam);
19 xTerm3 = (- mu / (2 * lam)) * math.sqrt(4 * mu * lam * y + mu ** 2 * y ** 2);
20 x = xTerm1 + xTerm2 + xTerm3;
21 z = random.uniform(0.0,1.0);
22 if z <= (mu / (mu + x)):
23 return x;
24 else:
25 return (mu ** 2 / x);
26
27 # Check input (TODO)
28 arg1 = float(sys.argv[1]); # first argument
29 desMean = arg1;
30
31 # Configure
32 lambdaFactor = 4; # spread factor; inversely proportional to variance
33
34 # Calculate delay
35 delay = randInvGau(desMean, desMean * lambdaFactor);
36 #print('DEBUG:delay:' + str(float(delay)));
37
38 # Sleep
39 time.sleep(float(delay));
40
41