forked from cawilliams/simple-conlang-generator
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRandomGen.py
More file actions
37 lines (32 loc) · 973 Bytes
/
RandomGen.py
File metadata and controls
37 lines (32 loc) · 973 Bytes
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
import random
import math
class YuleDistr(object):
def __init__(self):
pass
@staticmethod
def normalizeFreqs(freqs):
res = []
for i in range(0, len(freqs)):
if i == 0:
res.append(freqs[i])
else:
res.append(res[i-1]+freqs[i])
return res
@staticmethod
def randomGen(freqs):
temp = YuleDistr.normalizeFreqs(freqs)
if temp[len(temp)-1] >= 1:
return -1
else:
num = random.uniform(0, temp[len(temp)-1])
for i in range(0, len(temp)):
if i == 0 and num < temp[i]:
return i
elif num >= temp[i-1] and num < temp[i]:
return i
@staticmethod
def borodProb(num, rank):
if rank < 1 or num < 1:
return -1
else:
return (1/num)*(math.log(num+1)-math.log(rank))