One of n lines in a file: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 371:
}</lang>output
<pre>100561 99814 99816 99721 99244 99772 100790 100072 99997 100213</pre>
 
=={{header|C++}}==
{{works with|C++11}}
<lang cpp>#include <random>
#include <iostream>
#include <iterator>
#include <algorithm>
using namespace std;
 
mt19937 engine; //mersenne twister
 
unsigned int one_of_n(unsigned int n) {
unsigned int choice;
for(unsigned int i = 0; i < n; ++i) {
uniform_int_distribution<unsigned int> distribution(0, i);
if(!distribution(engine))
choice = i;
}
return choice;
}
 
int main() {
engine = mt19937(random_device()()); //seed random generator from system
unsigned int results[10] = {0};
for(unsigned int i = 0; i < 1000000; ++i)
results[one_of_n(10)]++;
ostream_iterator<unsigned int> out_it(cout, " ");
copy(results, results+10, out_it);
cout << '\n';
}
</lang>output
<pre>99981 99806 100190 99831 99833 100291 99356 100165 100279 100268</pre>
 
=={{header|C sharp|C#}}==
Line 452 ⟶ 420:
10 99726
</pre>
 
=={{header|C++}}==
{{works with|C++11}}
<lang cpp>#include <random>
#include <iostream>
#include <iterator>
#include <algorithm>
using namespace std;
 
mt19937 engine; //mersenne twister
 
unsigned int one_of_n(unsigned int n) {
unsigned int choice;
for(unsigned int i = 0; i < n; ++i) {
uniform_int_distribution<unsigned int> distribution(0, i);
if(!distribution(engine))
choice = i;
}
return choice;
}
 
int main() {
engine = mt19937(random_device()()); //seed random generator from system
unsigned int results[10] = {0};
for(unsigned int i = 0; i < 1000000; ++i)
results[one_of_n(10)]++;
ostream_iterator<unsigned int> out_it(cout, " ");
copy(results, results+10, out_it);
cout << '\n';
}
</lang>output
<pre>99981 99806 100190 99831 99833 100291 99356 100165 100279 100268</pre>
 
=={{header|Chapel}}==
Line 1,354:
end proc;
</lang>
 
 
 
=={{header|Mathematica}}==
Line 1,636 ⟶ 1,634:
++$freq[ one_of_n($size) - 1 ] for 1 .. $repeat;
print "@freq\n";</lang>
 
=={{header|Perl 6}}==
{{trans|Python}}
<lang perl6>sub one_of_n($n) {
my $choice;
$choice = $_ if .rand < 1 for 1 .. $n;
$choice - 1;
}
 
sub one_of_n_test($n = 10, $trials = 1_000_000) {
my @bins;
@bins[one_of_n($n)]++ for ^$trials;
@bins;
}
 
say one_of_n_test();</lang>
Output:
<pre>100288 100047 99660 99773 100256 99633 100161 100483 99789 99910</pre>
 
=={{header|Phix}}==
Line 1,932 ⟶ 1,912:
|#
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
{{trans|Python}}
<lang perl6>sub one_of_n($n) {
my $choice;
$choice = $_ if .rand < 1 for 1 .. $n;
$choice - 1;
}
 
sub one_of_n_test($n = 10, $trials = 1_000_000) {
my @bins;
@bins[one_of_n($n)]++ for ^$trials;
@bins;
}
 
say one_of_n_test();</lang>
Output:
<pre>100288 100047 99660 99773 100256 99633 100161 100483 99789 99910</pre>
 
=={{header|REXX}}==
10,333

edits