Unbias a random generator: Difference between revisions
Content added Content deleted
(Unbias a random generator en BASIC256) |
(Unbias a random generator en FreeBASIC) |
||
Line 852: | Line 852: | ||
5: 19.971% 49.987% |
5: 19.971% 49.987% |
||
6: 16.688% 50.097%</pre> |
6: 16.688% 50.097%</pre> |
||
=={{header|FreeBASIC}}== |
|||
{{trans|PureBasic}} |
|||
<lang freebasic> |
|||
Function randN (n As Ubyte) As Ubyte |
|||
If Int(Rnd * n) + 1 <> 1 Then Return 0 Else Return 1 |
|||
End Function |
|||
Function unbiased (n As Ubyte) As Ubyte |
|||
Dim As Ubyte a, b |
|||
Do |
|||
a = randN (n) |
|||
b = randN (n) |
|||
Loop Until a <> b |
|||
Return a |
|||
End Function |
|||
Const count = 100000 |
|||
Dim x As Ubyte |
|||
Randomize Timer |
|||
Print "Resultados de n";Chr(163);!"meros aleatorios sesgados e imparciales\n" |
|||
For n As Ubyte = 3 To 6 |
|||
Dim As Integer b_count(1) |
|||
Dim As Integer u_count(1) |
|||
For m As Integer = 1 To count |
|||
x = randN (n) |
|||
b_count(x) += 1 |
|||
x = unbiased (n) |
|||
u_count(x) += 1 |
|||
Next m |
|||
Print "N ="; n |
|||
Print " Biased =>", "#0="; Str(b_count(0)), "#1="; Str(b_count(1)), |
|||
Print Using "ratio = ##.##%"; (b_count(1) / count * 100) |
|||
Print "Unbiased =>", "#0="; Str(u_count(0)), "#1="; Str(u_count(1)), |
|||
Print Using "ratio = ##.##%"; (u_count(1) / count * 100) |
|||
Next n |
|||
Sleep |
|||
</lang> |
|||
{{out}} |
|||
<pre> |
|||
Resultados de números aleatorios sesgados e imparciales |
|||
N = 3 |
|||
Biased => #0=66633 #1=33367 ratio = 33.37% |
|||
Unbiased => #0=50029 #1=49971 ratio = 49.97% |
|||
N = 4 |
|||
Biased => #0=75068 #1=24932 ratio = 24.93% |
|||
Unbiased => #0=50107 #1=49893 ratio = 49.89% |
|||
N = 5 |
|||
Biased => #0=79998 #1=20002 ratio = 20.00% |
|||
Unbiased => #0=50049 #1=49951 ratio = 49.95% |
|||
N = 6 |
|||
Biased => #0=83195 #1=16805 ratio = 16.81% |
|||
Unbiased => #0=50026 #1=49974 ratio = 49.97% |
|||
</pre> |
|||
=={{header|GAP}}== |
=={{header|GAP}}== |