-
Notifications
You must be signed in to change notification settings - Fork 4
/
LubyGenerator.py
68 lines (53 loc) · 1.25 KB
/
LubyGenerator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import math
# List to store the luby numbers
l=[]
# Values that help to generate the Luby Sequence
mult = 1
minu = 0
def get_next_luby_number():
"""
Method to get the next luby number
Parameters:
None
Return:
the next Luby number in the sequence
"""
# Use the global variables
global l
global mult
global minu
size = len(l)
# Index of the element to be generated,
# pused into the list and returned
to_fill = size+1
if math.log(to_fill+1,2).is_integer():
# If the index is of the form 2^K -1,
# push mult into the queue and double it
# for the next push
l.append(mult)
mult *= 2
# This helps to fill the other indices
minu = size+1
else:
# If the index is not of the form 2^K-1,
# then the luby number is same as that at
# the index to_fill-minu-1
l.append(l[to_fill-minu-1])
return l[size]
def reset_luby():
"""
Method to reset the Luby Generator
to initial conditions.
Parameters:
None
Return:
None
"""
# Use the global variables
global l
global mult
global minu
# Reset it for the next use
l=[]
mult=1
minu=0