all: use math/rand/v2

This commit is contained in:
Hajime Hoshi 2024-09-12 01:03:53 +09:00
parent a4bfa6cb15
commit 4bccf9d009
24 changed files with 67 additions and 140 deletions

View File

@ -17,7 +17,7 @@ package convert_test
import (
"bytes"
"io"
"math/rand" // TODO: Use math/rand/v2 when the minimum supported version becomes Go 1.22.
"math/rand/v2"
"testing"
"unsafe"
@ -27,7 +27,7 @@ import (
func randInt16s(n int) []int16 {
r := make([]int16, n)
for i := range r {
r[i] = int16(rand.Intn(1<<16) - (1 << 15))
r[i] = int16(rand.IntN(1<<16) - (1 << 15))
}
return r
}

View File

@ -19,7 +19,7 @@ import (
"fmt"
"io"
"math"
"math/rand" // TODO: Use math/rand/v2 when the minimum supported version becomes Go 1.22.
"math/rand/v2"
"testing"
"github.com/hajimehoshi/ebiten/v2/audio/internal/convert"

View File

@ -17,8 +17,8 @@ package convert_test
import (
"bytes"
"fmt"
"io" // TODO: Use math/rand/v2 when the minimum supported version becomes Go 1.22.
"math/rand"
"io"
"math/rand/v2"
"testing"
"github.com/hajimehoshi/ebiten/v2/audio/internal/convert"
@ -101,7 +101,7 @@ func TestStereoI16(t *testing.T) {
func randBytes(n int) []byte {
r := make([]byte, n)
for i := range r {
r[i] = byte(rand.Intn(256))
r[i] = byte(rand.IntN(256))
}
return r
}

View File

@ -15,16 +15,9 @@
package twenty48
import (
"math/rand"
"time"
"github.com/hajimehoshi/ebiten/v2"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
const (
ScreenWidth = 420
ScreenHeight = 600

View File

@ -19,7 +19,7 @@ import (
"errors"
"image/color"
"log"
"math/rand"
"math/rand/v2"
"sort"
"strconv"
@ -261,9 +261,9 @@ func addRandomTile(tiles map[*Tile]struct{}, size int) error {
if len(availableCells) == 0 {
return errors.New("twenty48: there is no space to add a new tile")
}
c := availableCells[rand.Intn(len(availableCells))]
c := availableCells[rand.IntN(len(availableCells))]
v := 2
if rand.Intn(10) == 0 {
if rand.IntN(10) == 0 {
v = 4
}
x := c % size

View File

@ -20,9 +20,8 @@ import (
"image/color"
_ "image/jpeg"
_ "image/png"
"math/rand"
"math/rand/v2"
"strconv"
"time"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/colorm"
@ -139,10 +138,6 @@ type GameScene struct {
gameover bool
}
func init() {
rand.Seed(time.Now().UnixNano())
}
func NewGameScene() *GameScene {
return &GameScene{
field: &Field{},
@ -194,7 +189,7 @@ const (
func (s *GameScene) choosePiece() *Piece {
num := int(BlockTypeMax)
blockType := BlockType(rand.Intn(num) + 1)
blockType := BlockType(rand.IntN(num) + 1)
return Pieces[blockType]
}

View File

@ -18,7 +18,7 @@ import (
"fmt"
"image/color"
"log"
"math/rand"
"math/rand/v2"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"

View File

@ -17,8 +17,7 @@ package main
import (
"image/color"
"log"
"math/rand"
"time"
"math/rand/v2"
"github.com/hajimehoshi/ebiten/v2"
)
@ -102,7 +101,7 @@ func (g *Game) updateFireIntensityPerPixel(currentPixelIndex int) {
return
}
d := rand.Intn(3)
d := rand.IntN(3)
newI := int(g.indices[below]) - d
if newI < 0 {
newI = 0
@ -139,8 +138,6 @@ func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
}
func main() {
rand.Seed(time.Now().UnixNano())
ebiten.SetWindowSize(screenWidth*6, screenHeight*6)
ebiten.SetWindowTitle("Doom Fire (Ebitengine Demo)")
if err := ebiten.RunGame(NewGame()); err != nil {

View File

@ -20,8 +20,7 @@ import (
"image/color"
_ "image/png"
"log"
"math/rand"
"time"
"math/rand/v2"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
@ -29,10 +28,6 @@ import (
"github.com/hajimehoshi/ebiten/v2/inpututil"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
const (
screenWidth = 640
screenHeight = 480
@ -196,8 +191,8 @@ func NewGame() *Game {
s := &Sprite{
image: ebitenImage,
alphaImage: ebitenAlphaImage,
x: rand.Intn(screenWidth - w),
y: rand.Intn(screenHeight - h),
x: rand.IntN(screenWidth - w),
y: rand.IntN(screenHeight - h),
}
sprites = append(sprites, s)
}

View File

@ -24,8 +24,7 @@ import (
_ "image/png"
"log"
"math"
"math/rand"
"time"
"math/rand/v2"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/audio"
@ -44,10 +43,6 @@ var flagCRT = flag.Bool("crt", false, "enable the CRT effect")
//go:embed crt.go
var crtGo []byte
func init() {
rand.Seed(time.Now().UnixNano())
}
func floorDiv(x, y int) int {
d := x / y
if d*y == x || x >= 0 {
@ -150,7 +145,7 @@ func (g *Game) init() {
g.cameraY = 0
g.pipeTileYs = make([]int, 256)
for i := range g.pipeTileYs {
g.pipeTileYs[i] = rand.Intn(6) + 2
g.pipeTileYs[i] = rand.IntN(6) + 2
}
if g.audioContext == nil {

View File

@ -19,8 +19,7 @@ import (
"fmt"
"image/color"
"log"
"math/rand"
"time"
"math/rand/v2"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/examples/resources/fonts"
@ -94,10 +93,6 @@ func init() {
mplusFaceSource = s
}
func init() {
rand.Seed(time.Now().UnixNano())
}
type Game struct {
counter int
kanjiText string
@ -110,14 +105,14 @@ func (g *Game) Update() error {
g.kanjiText = ""
for j := 0; j < 6; j++ {
for i := 0; i < 12; i++ {
g.kanjiText += string(jaKanjis[rand.Intn(len(jaKanjis))])
g.kanjiText += string(jaKanjis[rand.IntN(len(jaKanjis))])
}
g.kanjiText += "\n"
}
g.kanjiTextColor.R = 0x80 + uint8(rand.Intn(0x7f))
g.kanjiTextColor.G = 0x80 + uint8(rand.Intn(0x7f))
g.kanjiTextColor.B = 0x80 + uint8(rand.Intn(0x7f))
g.kanjiTextColor.R = 0x80 + uint8(rand.IntN(0x7f))
g.kanjiTextColor.G = 0x80 + uint8(rand.IntN(0x7f))
g.kanjiTextColor.B = 0x80 + uint8(rand.IntN(0x7f))
g.kanjiTextColor.A = 0xff
}
g.counter++

View File

@ -16,8 +16,7 @@ package main
import (
"fmt"
"math/rand"
"time"
"math/rand/v2"
)
// Level represents a Game level.
@ -56,9 +55,6 @@ func NewLevel() (*Level, error) {
return nil, fmt.Errorf("failed to load embedded spritesheet: %s", err)
}
// Generate a unique permutation each time.
r := rand.New(rand.NewSource(time.Now().UTC().UnixNano()))
// Fill each tile with one or more sprites randomly.
l.tiles = make([][]*Tile, l.h)
for y := 0; y < l.h; y++ {
@ -66,7 +62,7 @@ func NewLevel() (*Level, error) {
for x := 0; x < l.w; x++ {
t := &Tile{}
isBorderSpace := x == 0 || y == 0 || x == l.w-1 || y == l.h-1
val := r.Intn(1000)
val := rand.IntN(1000)
switch {
case isBorderSpace || val < 275:
t.AddSprite(ss.Wall)

View File

@ -8,16 +8,11 @@ package main
import (
"log"
"math/rand"
"time"
"math/rand/v2"
"github.com/hajimehoshi/ebiten/v2"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
// World represents the game state.
type World struct {
area []bool
@ -39,8 +34,8 @@ func NewWorld(width, height int, maxInitLiveCells int) *World {
// init inits world with a random state.
func (w *World) init(maxLiveCells int) {
for i := 0; i < maxLiveCells; i++ {
x := rand.Intn(w.width)
y := rand.Intn(w.height)
x := rand.IntN(w.width)
y := rand.IntN(w.height)
w.area[y*w.width+x] = true
}
}

View File

@ -21,8 +21,7 @@ import (
"image"
_ "image/png"
"log"
"math/rand"
"time"
"math/rand/v2"
"github.com/hajimehoshi/ebiten/v2"
rmascot "github.com/hajimehoshi/ebiten/v2/examples/resources/images/mascot"
@ -60,10 +59,6 @@ func init() {
gopher3 = ebiten.NewImageFromImage(img3)
}
func init() {
rand.Seed(time.Now().UnixNano())
}
type mascot struct {
x16 int
y16 int
@ -105,8 +100,8 @@ func (m *mascot) Update() error {
}
// If the mascto is on the ground, cause an action in random.
if rand.Intn(60) == 0 && m.y16 == 0 {
switch rand.Intn(2) {
if rand.IntN(60) == 0 && m.y16 == 0 {
switch rand.IntN(2) {
case 0:
// Jump.
m.vy16 = -240

View File

@ -23,18 +23,13 @@ import (
_ "image/png"
"log"
"math"
"math/rand"
"time"
"math/rand/v2"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
"github.com/hajimehoshi/ebiten/v2/examples/resources/images"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
const (
screenWidth = 640
screenHeight = 480
@ -110,7 +105,7 @@ func (s *sprite) draw(screen *ebiten.Image) {
}
func newSprite(img *ebiten.Image) *sprite {
c := rand.Intn(50) + 300
c := rand.IntN(50) + 300
dir := rand.Float64() * 2 * math.Pi
a := rand.Float64() * 2 * math.Pi
s := rand.Float64()*0.1 + 0.4
@ -136,7 +131,7 @@ func (g *Game) Update() error {
g.sprites = list.New()
}
if g.sprites.Len() < 500 && rand.Intn(4) < 3 {
if g.sprites.Len() < 500 && rand.IntN(4) < 3 {
// Emit
g.sprites.PushBack(newSprite(smokeImage))
}

View File

@ -18,8 +18,7 @@ import (
"fmt"
"image/color"
"log"
"math/rand"
"time"
"math/rand/v2"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
@ -30,10 +29,6 @@ const (
screenHeight = 240
)
func init() {
rand.Seed(time.Now().UnixNano())
}
type Game struct {
offscreen *ebiten.Image
}
@ -46,12 +41,12 @@ func NewGame() *Game {
func (g *Game) Update() error {
s := g.offscreen.Bounds().Size()
x := rand.Intn(s.X)
y := rand.Intn(s.Y)
x := rand.IntN(s.X)
y := rand.IntN(s.Y)
c := color.RGBA{
byte(rand.Intn(256)),
byte(rand.Intn(256)),
byte(rand.Intn(256)),
byte(rand.IntN(256)),
byte(rand.IntN(256)),
byte(rand.IntN(256)),
byte(0xff),
}
g.offscreen.Set(x, y, c)

View File

@ -18,8 +18,7 @@ import (
"fmt"
"image/color"
"log"
"math/rand"
"time"
"math/rand/v2"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
@ -59,10 +58,6 @@ type Game struct {
level int
}
func init() {
rand.Seed(time.Now().UnixNano())
}
func (g *Game) collidesWithApple() bool {
return g.snakeBody[0].X == g.apple.X &&
g.snakeBody[0].Y == g.apple.Y
@ -130,8 +125,8 @@ func (g *Game) Update() error {
}
if g.collidesWithApple() {
g.apple.X = rand.Intn(xGridCountInScreen - 1)
g.apple.Y = rand.Intn(yGridCountInScreen - 1)
g.apple.X = rand.IntN(xGridCountInScreen - 1)
g.apple.Y = rand.IntN(yGridCountInScreen - 1)
g.snakeBody = append(g.snakeBody, Position{
X: g.snakeBody[len(g.snakeBody)-1].X,
Y: g.snakeBody[len(g.snakeBody)-1].Y,

View File

@ -21,7 +21,7 @@ import (
_ "image/png"
"log"
"math"
"math/rand"
"math/rand/v2"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
@ -119,9 +119,9 @@ func (g *Game) init() {
g.sprites.num = 500
for i := range g.sprites.sprites {
w, h := ebitenImage.Bounds().Dx(), ebitenImage.Bounds().Dy()
x, y := rand.Intn(screenWidth-w), rand.Intn(screenHeight-h)
vx, vy := 2*rand.Intn(2)-1, 2*rand.Intn(2)-1
a := rand.Intn(maxAngle)
x, y := rand.IntN(screenWidth-w), rand.IntN(screenHeight-h)
vx, vy := 2*rand.IntN(2)-1, 2*rand.IntN(2)-1
a := rand.IntN(maxAngle)
g.sprites.sprites[i] = &Sprite{
imageWidth: w,
imageHeight: h,

View File

@ -21,7 +21,7 @@ import (
_ "image/png"
"log"
"math"
"math/rand"
"math/rand/v2"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
@ -117,9 +117,9 @@ func (g *Game) init() {
g.sprites.num = 500
for i := range g.sprites.sprites {
w, h := ebitenImage.Bounds().Dx(), ebitenImage.Bounds().Dy()
x, y := rand.Intn(screenWidth-w), rand.Intn(screenHeight-h)
vx, vy := 2*rand.Intn(2)-1, 2*rand.Intn(2)-1
a := rand.Intn(maxAngle)
x, y := rand.IntN(screenWidth-w), rand.IntN(screenHeight-h)
vx, vy := 2*rand.IntN(2)-1, 2*rand.IntN(2)-1
a := rand.IntN(maxAngle)
g.sprites.sprites[i] = &Sprite{
imageWidth: w,
imageHeight: h,

View File

@ -20,8 +20,7 @@ import (
"fmt"
"image/color"
"log"
"math/rand"
"time"
"math/rand/v2"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
@ -127,8 +126,8 @@ type squiral struct {
func (s *squiral) spawn(game *Game) {
s.dead = false
rx := rand.Intn(width-4) + 2
ry := rand.Intn(height-4) + 2
rx := rand.IntN(width-4) + 2
ry := rand.IntN(height-4) + 2
for dx := -2; dx <= 2; dx++ {
for dy := -2; dy <= 2; dy++ {
@ -140,15 +139,15 @@ func (s *squiral) spawn(game *Game) {
}
}
s.speed = rand.Intn(5) + 1
s.speed = rand.IntN(5) + 1
s.pos.x = rx
s.pos.y = ry
s.dir = rand.Intn(4)
s.dir = rand.IntN(4)
game.colorCycle = (game.colorCycle + 1) % len(palettes[game.selectedPalette].colors)
s.col = palettes[game.selectedPalette].colors[game.colorCycle]
s.rot = rand.Intn(2)
s.rot = rand.IntN(2)
}
func (s *squiral) step(game *Game) {
@ -157,7 +156,7 @@ func (s *squiral) step(game *Game) {
}
x, y := s.pos.x, s.pos.y // shorthands
change := rand.Intn(1000)
change := rand.IntN(1000)
if change < 2 {
// On 0.2% of iterations, switch rotation direction.
s.rot = (s.rot + 1) % 2
@ -267,10 +266,6 @@ func (a *automaton) step(game *Game) {
}
}
func init() {
rand.Seed(time.Now().UnixNano())
}
type Game struct {
selectedPalette int
colorCycle int

View File

@ -17,8 +17,7 @@ package main
import (
"image/color"
"log"
"math/rand"
"time"
"math/rand/v2"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/vector"
@ -97,7 +96,6 @@ func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
}
func main() {
rand.Seed(time.Now().UnixNano())
ebiten.SetWindowSize(screenWidth, screenHeight)
ebiten.SetWindowTitle("Stars (Ebitengine Demo)")
if err := ebiten.RunGame(NewGame()); err != nil {

View File

@ -22,12 +22,11 @@ import (
_ "image/jpeg"
"log"
"math"
"math/rand"
"math/rand/v2"
"regexp"
"strconv"
"strings"
"sync"
"time"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
@ -55,7 +54,6 @@ var (
func init() {
flag.Parse()
rand.Seed(time.Now().UnixNano())
}
const (
@ -71,9 +69,9 @@ var (
func createRandomIconImage() image.Image {
const size = 32
rf := float64(rand.Intn(0x100))
gf := float64(rand.Intn(0x100))
bf := float64(rand.Intn(0x100))
rf := float64(rand.IntN(0x100))
gf := float64(rand.IntN(0x100))
bf := float64(rand.IntN(0x100))
img := ebiten.NewImage(size, size)
pix := make([]byte, 4*size*size)
for j := 0; j < size; j++ {

View File

@ -22,10 +22,9 @@ import (
"image/draw"
_ "image/png"
"math"
"math/rand"
"math/rand/v2"
"runtime"
"testing"
"time"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/examples/resources/images"
@ -37,10 +36,6 @@ import (
// maxImageSize is a maximum image size that should work in almost every environment.
const maxImageSize = 4096 - 2
func init() {
rand.Seed(time.Now().UnixNano())
}
func skipTooSlowTests(t *testing.T) bool {
if testing.Short() {
t.Skip("skipping test in short mode")
@ -4272,7 +4267,7 @@ func TestImageAntiAlias(t *testing.T) {
ebiten.BlendXor,
ebiten.BlendLighter,
} {
rnd := rand.New(rand.NewSource(0))
rnd := rand.New(rand.NewPCG(0, 0))
max := func(x, y, z byte) byte {
if x >= y && x >= z {
return x

View File

@ -16,7 +16,7 @@ package affine_test
import (
"math"
"math/rand"
"math/rand/v2"
"testing"
"github.com/hajimehoshi/ebiten/v2/internal/affine"