Knuth's algorithm S: Difference between revisions

Line 556:
 
=={{header|Elena}}==
<lang elena>#import system'dynamic.
#import system'dynamic.
#import extensions.
#import system'routines.
#import system'collections.
 
#class(extension) algorithmOp
{
#method s_of_n
[
#var counter := Integer new.
var n := self.
^ ArrayList new mix &into:
{
eval : ni
[
counter += 1.
if (thisself length < selfn)
? [ thisself += ni. ];
! [
((randomGenerator eval:counter < selfn) bool)
? [ thisself@(randomGenerator eval:selfn) := ni. ].
].
^ thisself array.
]
}.
Line 587:
}
 
#symbol program =
[
#var bin := Array new:10 set &every:(&index:n) [ Integer new ].
0 till:10000 &doEach: trial
[
#var s_of_n := 3 s_of_n.
0 till:10 &doEach:n
[
#var sample := s_of_n eval:n.
if (n == 9)
? [ sample run &each: i [ bin@i += 1. ]. ].
].
].
console writeLine:bin readChar.
].</lang>
{{out}}
Anonymous user