Last active
April 9, 2025 14:18
-
-
Save alex-lx/06fabd0b9a92830201fb34332da3e630 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
| import random | |
| def rand(low, up): | |
| if low < up: | |
| return rand(up, low) | |
| return low + random.random()*(up-low) | |
| def sample(m, n): | |
| low = 1 | |
| upper = m * 0.3 | |
| remain = m | |
| result = [] | |
| for i in range(n-1): | |
| remain_i = n - 1 - i | |
| l = max(low, remain - upper * remain_i) | |
| u = min(upper, remain - low * remain_i) | |
| x = rand(l, u) | |
| remain -= x | |
| result.append(x) | |
| result.append(remain) | |
| random.shuffle(result) | |
| return result | |
| m = 3000 | |
| n = 10 | |
| s = 10000 | |
| result = [0]*10 | |
| for i in range(s): | |
| result[int(sample(m, n)[0]) * 100 // (3*m)] += 1 | |
| for i, v in enumerate(result): | |
| print(('%3d: %.2f ' % (i*3*m//100, v * 100 / s)) + '#' '#'*(v * 100 // s)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment