RosettaCodeData/Task/Knuths-algorithm-S/11l/knuths-algorithm-s.11l

31 lines
667 B
Plaintext
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

T S_of_n_creator
Int n
i = 0
[Int] sample
F (n)
.n = n
F ()(item)
.i++
I .i <= .n
.sample.append(item)
E I random:(.i) < .n
.sample[random:(.n)] = item
V binarr = [0] * 10
V items = Array(0..9)
print(Single run samples for n = 3:)
V s_of_n = S_of_n_creator(3)
L(item) items
s_of_n(item)
print( Item: #. -> sample: #..format(item, s_of_n.sample))
L 100000
s_of_n = S_of_n_creator(3)
L(item) items
s_of_n(item)
L(s) s_of_n.sample
binarr[s]++
print("\nTest item frequencies for 100000 runs:\n "(enumerate(binarr).map((i, x) -> #.:#..format(i, x)).join("\n ")))