3454d244359baddb01a24a7b3826e141f5978005
[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.3
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 # Adjustments
12 lambdaFactor = 4; # spread factor; inversely proportional to variance
13
14 # Define functions
15 def randInvGau(mu, lam):
16 """Returns random variate of inverse gaussian distribution"""
17 # input: mu: mean of inverse gaussian distribution
18 # lam: shape parameter
19 # output: float sampled from inv. gaus. with range 0 to infinity, mean mu
20 # example: sample = float(randInvGau(1.0,4.0));
21 # Ref/Attrib: doi:10.1080/00031305.1976.10479147
22 nu = random.gauss(0,1);
23 y = nu ** 2;
24 xTerm1 = mu;
25 xTerm2 = mu ** 2 * y / (2 * lam);
26 xTerm3 = (- mu / (2 * lam)) * math.sqrt(4 * mu * lam * y + mu ** 2 * y ** 2);
27 x = xTerm1 + xTerm2 + xTerm3;
28 z = random.uniform(0.0,1.0);
29 if z <= (mu / (mu + x)):
30 return x;
31 else:
32 return (mu ** 2 / x);
33
34 # Check input (TODO)
35 arg1 = float(sys.argv[1]); # first argument
36 desMean = arg1;
37
38 # Calculate delay
39 delay = randInvGau(desMean, desMean * lambdaFactor);
40 #print('DEBUG:delay:' + str(float(delay)));
41
42 # Sleep
43 time.sleep(float(delay));
44
45 # Author: Steven Baltakatei Sandoal
46 # License: GPLv3+