Knuth's algorithm S: Difference between revisions
added RPL
(added RPL) |
|||
(3 intermediate revisions by 2 users not shown) | |||
Line 624:
=={{header|Elena}}==
ELENA
<syntaxhighlight lang="elena">import system'dynamic;
import extensions;
Line 641:
eval(i)
{
counter.append
if (
{
}
else
{
if(randomGenerator.nextInt
{
};
^
}
})
Line 661:
public program()
{
var bin := Array.allocate(10).populate::(n => new Integer());
for(int trial := 0
{
var s_of_n := 3.s_of_n();
for(int n := 0
{
var sample := s_of_n.eval
if (n == 9)
{ sample.forEach::(i){ bin[i].append
}
};
console.printLine
}</syntaxhighlight>
{{out}}
<pre>
3001,3052,3033,2973,2981,3060,3003,2975,2959,2963
</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="
var r = Random.new()
|