Commit | Line | Data |
---|---|---|
ae63d7db SBS |
1 | #!/usr/bin/env python3 |
2 | # Desc: Pauses a random amount of time. Random distribution is inverse gaussian. | |
0e82b3c2 | 3 | # Version: 0.0.3 |
ae63d7db SBS |
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 | ||
0e82b3c2 SBS |
11 | # Adjustments |
12 | lambdaFactor = 4; # spread factor; inversely proportional to variance | |
13 | ||
ae63d7db SBS |
14 | # Define functions |
15 | def randInvGau(mu, lam): | |
16 | """Returns random variate of inverse gaussian distribution""" | |
b914c401 SBS |
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)); | |
ae63d7db SBS |
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 | ||
ae63d7db SBS |
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 | ||
b914c401 SBS |
45 | # Author: Steven Baltakatei Sandoal |
46 | # License: GPLv3+ |