Seven-sided dice from five-sided dice: Difference between revisions
Content added Content deleted
(Updated the messages displayed by the checker.) |
(Seven-sided dice from five-sided dice en FreeBASIC) |
||
Line 765: | Line 765: | ||
Distribution potentially skewed for bucket 6 Expected: 142857 Actual: 142163 |
Distribution potentially skewed for bucket 6 Expected: 142857 Actual: 142163 |
||
Distribution potentially skewed for bucket 7 Expected: 142857 Actual: 142513</pre> |
Distribution potentially skewed for bucket 7 Expected: 142857 Actual: 142513</pre> |
||
=={{header|FreeBASIC}}== |
|||
{{trans|Liberty BASIC}} |
|||
<lang freebasic> |
|||
Function dice5() As Integer |
|||
Return Int(Rnd * 5) + 1 |
|||
End Function |
|||
Function dice7() As Integer |
|||
Dim As Integer temp |
|||
Do |
|||
temp = dice5() * 5 + dice5() -6 |
|||
Loop Until temp < 21 |
|||
Return (temp Mod 7) +1 |
|||
End Function |
|||
Function distCheck(n As Ulongint, delta As Double) As Ulongint |
|||
Dim As Ulongint a(n) |
|||
Dim As Ulongint maxBucket = 0 |
|||
Dim As Ulongint minBucket = 1000000 |
|||
For i As Ulongint = 1 To n |
|||
a(i) = dice5() |
|||
If a(i) > maxBucket Then maxBucket = a(i) |
|||
If a(i) < minBucket Then minBucket = a(i) |
|||
Next i |
|||
Dim As Ulongint nBuckets = maxBucket + 1 |
|||
Dim As Ulongint buckets(maxBucket) |
|||
For i As Ulongint = 1 To n |
|||
buckets(a(i)) += 1 |
|||
Next i |
|||
Dim As Ulongint expected = n / (maxBucket-minBucket+1) |
|||
Dim As Ulongint minVal = Int(expected*(1-delta)) |
|||
Dim As Ulongint maxVal = Int(expected*(1+delta)) |
|||
expected = Int(expected) |
|||
Print "minVal", "Expected", "maxVal" |
|||
Print minVal, expected, maxVal |
|||
Print "Bucket", "Counter", "pass/fail" |
|||
distCheck = true |
|||
For i As Ulongint = minBucket To maxBucket |
|||
Print i, buckets(i), Iif((minVal > buckets(i)) Or (buckets(i) > maxVal),"fail","") |
|||
If (minVal > buckets(i)) Or (buckets(i) > maxVal) Then Return false ''Else Return True |
|||
Next i |
|||
End Function |
|||
Dim Shared As Ulongint n = 1000000 |
|||
Print "Testing "; n; " times" |
|||
If Not(distCheck(n, 0.05)) Then Print "Test failed" Else Print "Test passed" |
|||
Sleep |
|||
</lang> |
|||
{{out}} |
|||
<pre> |
|||
Igual que la entrada de Liberty BASIC. |
|||
</pre> |
|||
=={{header|Go}}== |
=={{header|Go}}== |