RosettaCodeData/Task/Modified-random-distribution/11l/modified-random-distribution.11l

25 lines
714 B
Text
Raw Permalink Normal View History

2023-07-01 11:58:00 -04:00
F modifier(Float x) -> Float
R I x < 0.5 {2 * (.5 - x)} E 2 * (x - .5)
F modified_random_distribution((Float -> Float) modifier, Int n) -> [Float]
[Float] d
L d.len < n
V prob = random:()
I random:() < modifier(prob)
d.append(prob)
R d
V data = modified_random_distribution(modifier, 50'000)
V bins = 15
DefaultDict[Int, Int] counts
L(d) data
counts[d I/ (1 / bins)]++
V mx = max(counts.values())
print(" BIN, COUNTS, DELTA: HISTOGRAM\n")
Float? last
L(b, count) sorted(counts.items())
V delta = I last == N {N/A} E String(count - last)
print( #2.2, #4, #4: .format(Float(b) / bins, count, delta)(# * Int(40 * count / mx)))
last = count