Compare commits
No commits in common. "d7a36afe0d2e6ed90507d19ca84dfce34676420a" and "44698ca88e12768ef45debe102277f37982d302f" have entirely different histories.
d7a36afe0d
...
44698ca88e
16
main.go
16
main.go
@ -7,13 +7,14 @@ import (
|
|||||||
"math"
|
"math"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"os"
|
"os"
|
||||||
|
"runtime"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
randomMin = 0
|
randomMin = 0
|
||||||
randomMax = 9999999999999999
|
randomMax = 9999999999999999
|
||||||
numSamples = 100000000
|
numSamples = 10000000
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@ -33,8 +34,13 @@ func main() {
|
|||||||
log.Printf("generating numbers...")
|
log.Printf("generating numbers...")
|
||||||
|
|
||||||
rand.Seed(time.Now().UnixNano())
|
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++ {
|
for currentSample = 0; currentSample < numSamples; currentSample++ {
|
||||||
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
|
results[firstDigit(<-generatedNumbers)-1]++
|
||||||
}
|
}
|
||||||
|
|
||||||
statusTicker.Stop()
|
statusTicker.Stop()
|
||||||
@ -49,6 +55,12 @@ func main() {
|
|||||||
bufio.NewReader(os.Stdin).ReadBytes('\n')
|
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
|
// firstDigit returns the first/leftmost digit of the base10 representation of an integer
|
||||||
func firstDigit(x int) int {
|
func firstDigit(x int) int {
|
||||||
return int(math.Abs(float64(x)) / math.Pow(10, float64(numDigits(x)-1)))
|
return int(math.Abs(float64(x)) / math.Pow(10, float64(numDigits(x)-1)))
|
||||||
|
Loading…
Reference in New Issue
Block a user