Created
February 17, 2018 08:55
-
-
Save chawasit/a207a69b9ce45482aac5c0cac3c96c7b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from numpy.random import uniform, shuffle | |
| def generate(n=60, distributions=None): | |
| """Return a generated list of number by given list of range and distribution tuple | |
| eg. spec = [(2, 8, 0.7), (20, 30, 0.2), (35, 40, 0.1)] | |
| """ | |
| if distributions is None: | |
| distributions = [(2, 8, 0.7), (20, 30, 0.2), (35, 40, 0.1)] | |
| if n <= 0: | |
| raise Exception("n must greater than 0") | |
| if sum(map(lambda x: x[2], distributions)) - 1.0 > 1e-7: | |
| raise Exception("summation of probability must equal to 1") | |
| ret = [] | |
| for distribution in distributions: | |
| size = int(distribution[2] * n) | |
| ret.extend(map(int, uniform(distribution[0], distribution[1], size))) | |
| shuffle(ret) | |
| return ret |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment