Knuth's algorithm S: Difference between revisions

m (→‎{{header|REXX}}: added/changed comments and whitespace, used a template for the output, used a simpler function for COMMAS, added a header separator.)
Line 890:
 
=={{header|Julia}}==
<lang{{works with|Julia>|0.6}}
 
<lang julia>function makesofn(n::IntInteger)
buf = Any[]
buf = Vector{typeof(n)}(0)
i = 0
return function sofn(item)
i += 1
if i <= n
push!(buf, item)
else
j = rand(1:i)
if j <= n buf[j] = item end
buf[j] = item
end
end
return buf
end
return sofn
end
 
 
nhist = zeros(Int, 10)
for i_ in 1:10^5
 
for i in 1:10^5
kas = makesofn(3)
for j in 0:8 kas(j) end
for k in kas(j9) nhist[k+1] += 1 end
end
for k in kas(9)
nhist[k+1] += 1
end
end
 
println("Simulating sof3(0:9) 100000 times:")
for (i, c) in enumerate(nhist)
println@printf(@sprintf " %2d5d => %5d\n", i-1, c)
end</lang>
</lang>
 
{{out}}
<pre>Simulating sof3(0:9) 100000 times:
<pre>
0 → 29795
Simulating sof3(0:9) 100000 times:
01 => 2999429947
12 => 3002630227
23 => 3017330212
34 => 2959029763
45 => 2996729960
56 => 3010429809
67 => 3018530215
78 => 2976129948
89 => 3014730124</pre>
9 => 30053
</pre>
 
=={{header|Kotlin}}==
Anonymous user