Skip to content

Instantly share code, notes, and snippets.

@alex-lx
Last active April 9, 2025 14:18
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