Fix[entropy]: fix OneInA
function
This commit is contained in:
parent
7c3c116baa
commit
c7eafdbc52
@ -73,6 +73,9 @@ func RNG(n int) int {
|
|||||||
// OneInA generates a random number with a maximum of 'million' (input int).
|
// OneInA generates a random number with a maximum of 'million' (input int).
|
||||||
// If the resulting random number is equal to 1, then the result is true.
|
// If the resulting random number is equal to 1, then the result is true.
|
||||||
func OneInA(million int) bool {
|
func OneInA(million int) bool {
|
||||||
|
if million == 1 {
|
||||||
|
return true
|
||||||
|
}
|
||||||
return RNG(million) == 1
|
return RNG(million) == 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,9 +6,13 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var dupCount = 0
|
||||||
|
|
||||||
func check[T comparable](zero T, one T, t *testing.T) {
|
func check[T comparable](zero T, one T, t *testing.T) {
|
||||||
if zero == one {
|
if zero == one {
|
||||||
|
dupCount++
|
||||||
t.Errorf("hit a duplicate! %v == %v", zero, one)
|
t.Errorf("hit a duplicate! %v == %v", zero, one)
|
||||||
|
t.Logf("duplicates so far: %d", dupCount)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -33,6 +37,18 @@ func Test_RNG(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Test_OneInA(t *testing.T) {
|
||||||
|
for n := 0; n < 100; n++ {
|
||||||
|
yes := ""
|
||||||
|
if OneInA(1) {
|
||||||
|
yes = "hello"
|
||||||
|
}
|
||||||
|
if yes != "hello" {
|
||||||
|
t.Fatalf("OneInA failed to trigger when provided '1' as an argument")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func randStrChecks(zero, one string, t *testing.T, intendedLength int) {
|
func randStrChecks(zero, one string, t *testing.T, intendedLength int) {
|
||||||
if len(zero) != len(one) {
|
if len(zero) != len(one) {
|
||||||
t.Fatalf("RandStr output length inconsistency, len(zero) is %d but wanted len(one) which is %d", len(zero), len(one))
|
t.Fatalf("RandStr output length inconsistency, len(zero) is %d but wanted len(one) which is %d", len(zero), len(one))
|
||||||
|
Loading…
Reference in New Issue
Block a user