Knuth's algorithm S: Difference between revisions

added RPL
(added RPL)
 
(3 intermediate revisions by 2 users not shown)
Line 624:
 
=={{header|Elena}}==
ELENA 56.0x :
<syntaxhighlight lang="elena">import system'dynamic;
import extensions;
Line 641:
eval(i)
{
counter.append:(1);
 
if (__targetweak self.Length < n)
{
__targetweak self.append:(i)
}
else
{
if(randomGenerator.nextInt:(counter) < n)
{ __targetweak self[randomGenerator.nextInt:(n)] := i }
};
 
^ __targetweak self.Value
}
})
Line 661:
public program()
{
var bin := Array.allocate(10).populate::(n => new Integer());
for(int trial := 0,; trial < 10000,; trial += 1)
{
var s_of_n := 3.s_of_n();
for(int n := 0,; n < 10,; n += 1)
{
var sample := s_of_n.eval:(n);
if (n == 9)
{ sample.forEach::(i){ bin[i].append:(1) } }
}
};
console.printLine:(bin).readChar()
}</syntaxhighlight>
{{out}}
<pre>
3001,3052,3033,2973,2981,3060,3003,2975,2959,2963
3050,3029,3041,2931,3040,2952,2901,2984,3069,3003
</pre>
 
Line 1,788:
frequency of the 8 digit is: 29,976
frequency of the 9 digit is: 29,871
</pre>
 
=={{header|RPL}}==
This is an idiomatic adaptation of the algorithm: SCREA initializes 2 persistent variables: S contains the sample and SPAR the algorithm parameters (n and i)
{{works with|RPL|HP49-C}}
« 0 2 →LIST '<span style="color:green">SPAR</span>' STO { } '<span style="color:green">S</span>' STO
» '<span style="color:blue">SCREA</span>' STO
« <span style="color:green">SPAR</span> EVAL
'''CASE'''
DUP2 > '''THEN''' DROP2 '<span style="color:green">S</span>' STO+ '''END'''
/ →NUM RAND ≥ '''THEN''' <span style="color:green">S</span> DUP SIZE RAND * CEIL ROT PUT '<span style="color:green">S</span>' STO '''END'''
DROP '''END'''
'<span style="color:green">SPAR</span>' 2 DUP2 GET 1 + PUT <span style="color:green">S</span>
» '<span style="color:blue">SOFN</span>' STO
« { } → sample
« { 10 } 0 CON
1 1000 '''START'''
3 <span style="color:blue">SCREA</span>
0
0 9 '''FOR''' k
DROP k <span style="color:blue">SOFN</span>
'''NEXT'''
'sample' STO
« sample k POS » 'k' 0 9 1 SEQ
NOT NOT AXL +
'''NEXT'''
» » '<span style="color:blue">TASK</span>' STO
{{out}}
<pre>
1: [ 206. 218. 235. 309. 359. 329. 327. 324. 359. 334. ]
</pre>
 
Line 2,072 ⟶ 2,104:
=={{header|Wren}}==
{{trans|Go}}
<syntaxhighlight lang="ecmascriptwren">import "random" for Random
 
var r = Random.new()
1,150

edits