-
Notifications
You must be signed in to change notification settings - Fork 0
/
LCG.py
33 lines (26 loc) · 1.1 KB
/
LCG.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
import time
randomValue = int(time.time())
def randint(start, end): #MENGENERATE RANDOM INTEGER PADA RANGE DARI START SAMPAI END SECARA INKLUSIF.
global randomValue
if end >= start:
m = 2**32
a = 1664525
c = 1013904224
randomValue = ((a*randomValue+c) % m) # new random value is for the next seed.
r = randomValue/m # KARENA NILAI 0 <= x < m, maka 0 <= r < 1 dan start <= start + ((end-start)*r)//1 + 1 <= end
result = start + int(((end-start+1)*r)//1)
return result
else:
return 0
def randsample(n, start, end): #MENGENERATE LIST BERISI RANDOM INTEGER DENGAN PANJANG N PADA RANGE DARI START SAMPAI END SECARA INKLUSIF.
lst = [0 for i in range(n)]
for i in range(1, n+1):
lst[i-1] = randint(start, end)
return lst
if __name__ == '__main__':
print('15 random integer pada interval [1,5]: ')
for i in range(15):
print(randint(1,5), end = ' ')
print()
print('List berisi 10 random integer pada interval [1,4]: ')
print(randsample(10,1,4))