-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandomtypes.py
35 lines (29 loc) · 911 Bytes
/
randomtypes.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import random
MAX_RAND_INT = 2**31
MAX_RAND_FLOAT = 300.0
class RandomType(object):
pass
def RandomInt(upper_bound=MAX_RAND_INT):
class ClassRandomInt(int, RandomType):
def __new__(T):
value = random.randint(0, upper_bound)
return int.__new__(T, value)
ClassRandomInt.__name__ = 'RandomInt'
return ClassRandomInt
def RandomBool():
class ClassRandomBool(int, RandomType):
def __new__(T):
return int.__new__(T, random.randint(0, 1))
def __repr__(self):
return 'True' if self else 'False'
ClassRandomBool.__name__ = 'RandomBool'
return ClassRandomBool
def RandomFloat(upper_bound=MAX_RAND_FLOAT):
class ClassRandomFloat(float, RandomType):
def __new__(T):
value = random.random()*upper_bound
return float.__new__(T, value)
ClassRandomFloat.__name__ = 'RandomFloat'
return ClassRandomFloat