+# This is all encapsulated not just for logging but because someday
+# we'll probably want to replace it with something like an LCG that
+# can be forward-ported off Python. Thee only function we need is one to
+# return a variate uniformly-distributed over [0, 1).
+
+import random
+
+class randomizer:
+ @staticmethod
+ def withprob(p):
+ v = random.random()
+ if logfp:
+ logfp.write("#withprob(%.2f) -> %s\n" % (p, v < p))
+ return v < p
+
+ @staticmethod
+ def integer(*args):
+ s = random.randrange(*args)
+ if logfp:
+ logfp.write("#randrange%s -> %s\n" % (args, s))
+ return s
+
+ @staticmethod
+ def real(*args):
+ v = random.random()
+ if len(args) == 1:
+ v *= args[0] # from [0, args[0])
+ elif len(args) == 2:
+ v = args[0] + v*(args[1]-args[0]) # from [args[0], args[1])
+ if logfp:
+ logfp.write("#real%s -> %f\n" % (args, v))
+ return v
+
+ @staticmethod
+ def seed(n):
+ if logfp:
+ logfp.write("#seed(%d)\n" % n)
+ random.seed(n)
+