Unbias a random generator: Difference between revisions

m
m (→‎{{header|J}}: grammar describing randomness is ... strange)
 
(5 intermediate revisions by 4 users not shown)
Line 573:
5: 19.958% 49.987%
6: 16.660% 49.890%</pre>
 
=={{header|EasyLang}}==
{{trans|Java}}
<syntaxhighlight>
func biased n .
return if randomf < 1 / n
.
func unbiased n .
repeat
a = biased n
b = biased n
until a <> b
.
return a
.
m = 50000
for n = 3 to 6
c1 = 0
c2 = 0
for i to m
c1 += biased n
c2 += unbiased n
.
print n & ": " & 100 * c1 / m & " " & 100 * c2 / m
.
</syntaxhighlight>
{{out}}
<pre>
3: 33.10 50.14
4: 25.03 50.14
5: 19.81 50.04
6: 16.75 49.83
</pre>
 
=={{header|Elena}}==
{{trans|C#}}
ELENA 46.x :
<syntaxhighlight lang="elena">import extensions;
Line 601 ⟶ 634:
public program()
{
for(int n := 3,; n <= 6,; n += 1)
{
int biasedZero := 0;
Line 608 ⟶ 641:
int unbiasedOne := 0;
for(int i := 0,; i < 100000,; i += 1)
{
if(n.randN()) { biasedOne += 1 } else { biasedZero += 1 };
Line 915 ⟶ 948:
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Unbias_a_random_generator}}
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text. Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for storage and transfer purposes more than visualization and edition.
 
'''Solution'''
 
[[File:Fōrmulæ - Unbias a random generator 01.png]]
 
[[File:Fōrmulæ - Unbias a random generator 02.png]]
 
[[File:Fōrmulæ - Unbias a random generator 03.png]]
 
'''Test cases'''
 
[[File:Fōrmulæ - Unbias a random generator 04.png]]
Programs in Fōrmulæ are created/edited online in its [https://formulae.org website], However they run on execution servers. By default remote servers are used, but they are limited in memory and processing power, since they are intended for demonstration and casual use. A local server can be downloaded and installed, it has no limitations (it runs in your own computer). Because of that, example programs can be fully visualized and edited, but some of them will not run if they require a moderate or heavy computation/memory resources, and no local server is being used.
 
[[File:Fōrmulæ - Unbias a random generator 05.png]]
In '''[https://formulae.org/?example=Unbias_a_random_generator this]''' page you can see the program(s) related to this task and their results.
 
=={{header|GAP}}==
Line 2,314 ⟶ 2,357:
{{trans|Kotlin}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "random" for Random
import "./fmt" for Fmt
 
var rand = Random.new()
1,981

edits