Knuth's algorithm S: Difference between revisions

CoffeeScript
(→‎{{header|Go}}: library changes)
(CoffeeScript)
Line 154:
30247
</pre>
 
=={{header|CoffeeScript}}==
<lang coffeescript>
s_of_n_creator = (n) ->
arr = []
cnt = 0
(elem) ->
cnt += 1
if cnt <= n
arr.push elem
else
pos = Math.floor(Math.random() * cnt)
if pos < n
arr[pos] = elem
arr.sort()
 
sample_size = 3
range = [0..9]
num_trials = 100000
 
counts = {}
 
for digit in range
counts[digit] = 0
for i in [1..num_trials]
s_of_n = s_of_n_creator(sample_size)
for digit in range
sample = s_of_n(digit)
for digit in sample
counts[digit] += 1
 
for digit in range
console.log digit, counts[digit]
</lang>
output
<lang>
> coffee knuth_sample.coffee
0 29899
1 29841
2 29930
3 30058
4 29932
5 29948
6 30047
7 30114
8 29976
9 30255
</lang>
 
 
=={{header|Common Lisp}}==
Anonymous user