Improve support for functions #1
This commit is contained in:
17
smnp/environment/function/rand.py
Normal file
17
smnp/environment/function/rand.py
Normal file
@@ -0,0 +1,17 @@
|
||||
import random as r
|
||||
|
||||
|
||||
def random(args, env):
|
||||
if not all(isinstance(x, list) and len(x) == 2 and isinstance(x[0], float) for x in args):
|
||||
return # not valid signature
|
||||
if sum([x[0] for x in args]) != 1.0:
|
||||
return # not sums to 100%
|
||||
choice = r.random()
|
||||
acc = 0
|
||||
for e in args:
|
||||
acc += e[0]
|
||||
if choice <= acc:
|
||||
return e[1]
|
||||
|
||||
|
||||
#TODO: sample
|
||||
Reference in New Issue
Block a user