Knuth's algorithm S: Difference between revisions

(Added Kotlin)
Line 556:
 
=={{header|Elena}}==
<lang elena>#import system'dynamic.
#import extensions.
#import system'routines.
#import system'collections.
 
extension algorithmOp
Line 568:
var n := self.
^ ArrayList new; mix &intomixInto:
{
eval : i
[
counter += append:1.
if (self length < n)
[ self += append:i. ];
[
(if(randomGenerator eval:counter < n) bool)
? [ self@([randomGenerator eval:n)] := i. ].
].
^ self array.
]
}.
Line 589:
program =
[
var bin := Array new:10; set &every:populate(&index:n) [( Integer new ]).
0 till:10000 &doEachdo(: trial)
[
var s_of_n := 3 s_of_n.
0 till:10 &doEachdo(:n)
[
var sample := s_of_n eval:n.
if (n == 9)
[ sample run &eachforEach(: i) [ bin@[i] += append:1. ]. ].
].
].
console writeLineprintLine:bin; readChar.
].</lang>
{{out}}
Anonymous user