Compare commits

...

3 Commits

2 changed files with 3 additions and 15 deletions

View File

@ -27,5 +27,5 @@ $ ./benfords-law.exe
Press 'Enter' to continue...
```
This shows that Benford's law only works when the data is not random, such as natural data gathered in real life. This is because natural data is generated following a power law, which is common in nature.
This shows that Benford's law only works when the data is not random, such as natural data gathered in real life. This is because natural data is generated following a [power law](https://en.wikipedia.org/wiki/Power_law), which is common in nature.

16
main.go
View File

@ -7,14 +7,13 @@ import (
"math"
"math/rand"
"os"
"runtime"
"time"
)
const (
randomMin = 0
randomMax = 9999999999999999
numSamples = 10000000
numSamples = 100000000
)
func main() {
@ -34,13 +33,8 @@ func main() {
log.Printf("generating numbers...")
rand.Seed(time.Now().UnixNano())
generatedNumbers := make(chan int, 1024)
for i := 0; i < runtime.NumCPU(); i++ {
go generatorWorker(generatedNumbers)
}
for currentSample = 0; currentSample < numSamples; currentSample++ {
results[firstDigit(<-generatedNumbers)-1]++
results[firstDigit(rand.Intn(randomMax-randomMin+1)+randomMin)-1]++ // We must use Intn instead of Int because from Base10's perspective, integers cut off at a really weird spot
}
statusTicker.Stop()
@ -55,12 +49,6 @@ func main() {
bufio.NewReader(os.Stdin).ReadBytes('\n')
}
func generatorWorker(returnChannel chan int) {
for {
returnChannel <- rand.Intn(randomMax-randomMin+1) + randomMin // We must use Intn instead of Int because from Base10's perspective, integers cut off at a really weird spot
}
}
// firstDigit returns the first/leftmost digit of the base10 representation of an integer
func firstDigit(x int) int {
return int(math.Abs(float64(x)) / math.Pow(10, float64(numDigits(x)-1)))