Jump to content

One of n lines in a file: Difference between revisions

Add Forth
(First implementation)
(Add Forth)
Line 752:
9 chosen 100058 times
10 chosen 99965 times</pre>
=={{header|Forth}}==
{{works with|GNU Forth}}for random.fs and 1/f
<lang forth>require random.fs
 
: frnd
rnd 0 d>f [ s" MAX-U" environment? drop 0 d>f 1/f ] fliteral f* ;
: u>f 0 d>f ;
: one_of_n ( u1 -- u2 )
1 swap 1+ 2 ?do frnd i u>f 1/f f< if drop i then loop ;
 
create hist 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , does> swap cells + ;
: simulate 1000000 0 do 1 10 one_of_n 1- hist +! loop ;
: .hist cr 10 0 do i 1+ 2 .r ." : " i hist @ . cr loop ;
 
simulate .hist bye</lang>
{{out}}
<pre>&gt; gforthamd64 rosetta_one_of_n.fs
 
1: 99381
2: 99970
3: 99793
4: 100035
5: 100195
6: 100147
7: 99583
8: 100135
9: 100309
10: 100452</pre>
=={{header|Go}}==
<lang go>package main
Cookies help us deliver our services. By using our services, you agree to our use of cookies.