Fixing a few places where I was using "crypto/rand"

to use "math/rand" with a proper seeding instead.
This commit is contained in:
Dan Buch 2012-12-20 00:34:24 -05:00
parent 7a40632ca8
commit e81b35471e
2 changed files with 10 additions and 27 deletions

View File

@ -1,12 +1,12 @@
package main
import (
"crypto/rand"
"fmt"
"math"
"math/big"
"math/rand"
"os"
"strconv"
"time"
)
import (
@ -21,7 +21,9 @@ func main() {
os.Exit(1)
}
nflips, err := strconv.ParseInt(os.Args[1], 10, 32)
rand.Seed(time.Now().UTC().UnixNano())
nflips, err := strconv.Atoi(os.Args[1])
if err != nil {
fmt.Println("Wat.", err)
os.Exit(2)
@ -30,16 +32,8 @@ func main() {
heads := algs4.NewCounter("heads")
tails := algs4.NewCounter("tails")
randMax := big.NewInt(2)
for t := int64(0); t < nflips; t += int64(1) {
n, err := rand.Int(rand.Reader, randMax)
if err != nil {
fmt.Println("Ugh:", err)
os.Exit(4)
}
if n.Int64() < 1 {
for t := 0; t < nflips; t++ {
if rand.Float64() > 0.5 {
heads.Increment()
} else {
tails.Increment()

View File

@ -1,32 +1,21 @@
package algs4
import (
"crypto/rand"
"fmt"
"math/big"
)
const ROLL_SIDES = 6
func Rolls(nrolls int64) ([]*Counter, error) {
var err error
rolls := make([]*Counter, ROLL_SIDES+1)
for i := 1; i <= ROLL_SIDES; i += 1 {
rolls[i] = NewCounter(fmt.Sprintf("%d's", i))
}
var result *big.Int
randMax := big.NewInt(ROLL_SIDES)
randMax := float64(ROLL_SIDES)
for t := int64(0); t < nrolls; t += 1 {
result, err = rand.Int(rand.Reader, randMax)
if err != nil {
return nil, err
}
rolls[int(result.Int64())+1].Increment()
for t := int64(0); t < nrolls; t++ {
rolls[int(RandomUniform(float64(0.0), randMax))+1].Increment()
}
return rolls, nil