Use crypto/rand to generate random numbers

This commit is contained in:
Trevor Slocum 2023-11-03 12:38:48 -07:00
parent 8646e4cc39
commit 7e7aed5776
3 changed files with 50 additions and 15 deletions

View file

@ -3,7 +3,6 @@ package main
import (
"bufio"
"bytes"
"math/rand"
"time"
"code.rocket9labs.com/tslocum/bgammon"
@ -22,7 +21,6 @@ type serverGame struct {
rematch int
rejoin1 bool
rejoin2 bool
r *rand.Rand
*bgammon.Game
}
@ -33,7 +31,6 @@ func newServerGame(id int) *serverGame {
created: now,
lastActive: now,
Game: bgammon.NewGame(),
r: rand.New(rand.NewSource(time.Now().UnixNano() + rand.Int63n(1000000))),
}
}
@ -50,14 +47,14 @@ func (g *serverGame) roll(player int) bool {
} else {
secondRoll = true
}
g.Roll1 = g.r.Intn(6) + 1
g.Roll1 = randInt(6) + 1
} else {
if g.Roll2 != 0 {
return false
} else {
secondRoll = true
}
g.Roll2 = g.r.Intn(6) + 1
g.Roll2 = randInt(6) + 1
}
if secondRoll && g.Started.IsZero() {
g.Started = time.Now()
@ -72,7 +69,8 @@ func (g *serverGame) roll(player int) bool {
return false
}
g.Roll1, g.Roll2 = g.r.Intn(6)+1, g.r.Intn(6)+1
g.Roll1 = randInt(6) + 1
g.Roll2 = randInt(6) + 1
return true
}
@ -184,8 +182,7 @@ func (g *serverGame) addClient(client *serverClient) (bool, string) {
client.playerNumber = 1
playerNumber = 1
default:
i := rand.Intn(2)
if i == 0 {
if randInt(2) == 0 {
g.client1 = client
g.Player1.Name = string(client.name)
client.playerNumber = 1

View file

@ -10,15 +10,22 @@ import (
func main() {
var (
tcpAddress string
wsAddress string
debug int
tcpAddress string
wsAddress string
debug int
rollStatistics bool
)
flag.StringVar(&tcpAddress, "tcp", "localhost:1337", "TCP listen address")
flag.StringVar(&wsAddress, "ws", "localhost:1338", "WebSocket listen address")
flag.IntVar(&debug, "debug", 0, "print debug information and serve pprof on specified port")
flag.BoolVar(&rollStatistics, "statistics", false, "print dice roll statistics and exit")
flag.Parse()
if rollStatistics {
printRollStatistics()
return
}
if tcpAddress == "" && wsAddress == "" {
log.Fatal("Error: A TCP and/or WebSocket listen address must be specified.")
}
@ -38,3 +45,26 @@ func main() {
}
select {}
}
func printRollStatistics() {
var oneSame, doubles int
var lastroll1, lastroll2 int
total := 1000000
for i := 0; i < total; i++ {
roll1 := randInt(6) + 1
roll2 := randInt(6) + 1
if roll1 == lastroll1 || roll1 == lastroll2 || roll2 == lastroll1 || roll2 == lastroll2 {
oneSame++
}
if roll1 == roll2 {
doubles++
}
lastroll1, lastroll2 = roll1, roll2
}
log.Printf("total: %d, one same: %d (%.0f%%), doubles: %d (%.0f%%)", total, oneSame, float64(oneSame)/float64(total)*100, doubles, float64(doubles)/float64(total)*100)
}

View file

@ -2,9 +2,10 @@ package main
import (
"bytes"
"crypto/rand"
"fmt"
"log"
"math/rand"
"math/big"
"net"
"net/http"
"regexp"
@ -18,7 +19,7 @@ import (
const clientTimeout = 40 * time.Second
const allowDebugCommands = false
var allowDebugCommands bool
var (
onlyNumbers = regexp.MustCompile(`^[0-9]+$`)
@ -262,8 +263,7 @@ func (s *server) handleNewClientIDs() {
// randomUsername returns a random guest username, and assumes clients are already locked.
func (s *server) randomUsername() []byte {
for {
i := 100 + rand.Intn(900)
name := []byte(fmt.Sprintf("Guest%d", i))
name := []byte(fmt.Sprintf("Guest%d", 100+randInt(900)))
if s.clientByUsername(name) == nil {
return name
@ -1081,3 +1081,11 @@ COMMANDS:
}
}
}
func randInt(max int) int {
i, err := rand.Int(rand.Reader, big.NewInt(int64(max)))
if err != nil {
panic(err)
}
return int(i.Int64())
}