Skip to content

Commit

Permalink
all: migrate to math/rand/v2
Browse files Browse the repository at this point in the history
Fixes go-hep#1025

Signed-off-by: Sebastien Binet <[email protected]>
  • Loading branch information
sbinet committed Feb 17, 2025
1 parent e2a54ec commit 0d92fc6
Show file tree
Hide file tree
Showing 49 changed files with 186 additions and 327 deletions.
4 changes: 2 additions & 2 deletions fads/btagging.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ package fads

import (
"math"
"math/rand/v2"
"reflect"
"sync"

"go-hep.org/x/hep/fmom"
"go-hep.org/x/hep/fwk"
"golang.org/x/exp/rand"
"gonum.org/v1/gonum/stat/distuv"
)

Expand Down Expand Up @@ -74,7 +74,7 @@ func (tsk *BTagging) Configure(ctx fwk.Context) error {
return err
}

tsk.src = rand.New(rand.NewSource(tsk.seed))
tsk.src = rand.New(rand.NewPCG(tsk.seed, tsk.seed))
tsk.flat = distuv.Uniform{Min: 0, Max: 1, Src: tsk.src}
return err
}
Expand Down
4 changes: 2 additions & 2 deletions fads/calo.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ package fads
import (
"fmt"
"math"
"math/rand/v2"
"reflect"
"sort"
"sync"

"go-hep.org/x/hep/fwk"
"golang.org/x/exp/rand"
"gonum.org/v1/gonum/stat/distuv"
)

Expand Down Expand Up @@ -206,7 +206,7 @@ func (tsk *Calorimeter) Configure(ctx fwk.Context) error {
return err
}

tsk.src = rand.New(rand.NewSource(tsk.seed))
tsk.src = rand.New(rand.NewPCG(tsk.seed, tsk.seed))
tsk.gauss = distuv.Normal{Mu: 0, Sigma: 1, Src: tsk.src}
return err
}
Expand Down
4 changes: 2 additions & 2 deletions fads/efficiency.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
package fads

import (
"math/rand/v2"
"reflect"
"sync"

"go-hep.org/x/hep/fwk"
"golang.org/x/exp/rand"
"gonum.org/v1/gonum/stat/distuv"
)

Expand Down Expand Up @@ -42,7 +42,7 @@ func (tsk *Efficiency) Configure(ctx fwk.Context) error {

func (tsk *Efficiency) StartTask(ctx fwk.Context) error {
var err error
src := rand.New(rand.NewSource(tsk.seed))
src := rand.New(rand.NewPCG(tsk.seed, tsk.seed))
tsk.dist = distuv.Uniform{Min: 0, Max: 1, Src: src}
return err
}
Expand Down
4 changes: 2 additions & 2 deletions fads/energy_smearing.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ package fads

import (
"math"
"math/rand/v2"
"reflect"
"sync"

"go-hep.org/x/hep/fmom"
"go-hep.org/x/hep/fwk"
"golang.org/x/exp/rand"
"gonum.org/v1/gonum/stat/distuv"
)

Expand Down Expand Up @@ -45,7 +45,7 @@ func (tsk *EnergySmearing) Configure(ctx fwk.Context) error {

func (tsk *EnergySmearing) StartTask(ctx fwk.Context) error {
var err error
tsk.src = rand.New(rand.NewSource(tsk.seed))
tsk.src = rand.New(rand.NewPCG(tsk.seed, tsk.seed))
return err
}

Expand Down
4 changes: 2 additions & 2 deletions fads/momentum_smearing.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ package fads

import (
"math"
"math/rand/v2"
"reflect"
"sync"

"go-hep.org/x/hep/fmom"
"go-hep.org/x/hep/fwk"
"golang.org/x/exp/rand"
"gonum.org/v1/gonum/stat/distuv"
)

Expand Down Expand Up @@ -45,7 +45,7 @@ func (tsk *MomentumSmearing) Configure(ctx fwk.Context) error {

func (tsk *MomentumSmearing) StartTask(ctx fwk.Context) error {
var err error
tsk.src = rand.New(rand.NewSource(tsk.seed))
tsk.src = rand.New(rand.NewPCG(tsk.seed, tsk.seed))
return err
}

Expand Down
4 changes: 2 additions & 2 deletions fads/tautagging.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ package fads

import (
"math"
"math/rand/v2"
"reflect"
"sync"

"go-hep.org/x/hep/fmom"
"go-hep.org/x/hep/fwk"
"golang.org/x/exp/rand"
"gonum.org/v1/gonum/stat/distuv"
)

Expand Down Expand Up @@ -90,7 +90,7 @@ func (tsk *TauTagging) Configure(ctx fwk.Context) error {
return err
}

tsk.src = rand.New(rand.NewSource(tsk.seed))
tsk.src = rand.New(rand.NewPCG(tsk.seed, tsk.seed))
tsk.flat = distuv.Uniform{Min: 0, Max: 1, Src: tsk.src}
return err
}
Expand Down
5 changes: 2 additions & 3 deletions fit/curve_nd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
package fit_test

import (
"math/rand/v2"
"testing"

"golang.org/x/exp/rand"
)

func TestCurve2D(t *testing.T) {
Expand All @@ -18,7 +17,7 @@ func genData2D(n0, n1 int, f func(x, ps []float64) float64, ps []float64, x0min,
var (
xdata = make([][]float64, n0*n1)
ydata = make([]float64, n0*n1)
rnd = rand.New(rand.NewSource(1234))
rnd = rand.New(rand.NewPCG(1234, 1234))
x0step = (x0max - x0min) / float64(n0)
x1step = (x1max - x1min) / float64(n1)
p = make([]float64, len(ps))
Expand Down
6 changes: 3 additions & 3 deletions fit/hist_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (
"image/color"
"log"
"math"
"math/rand/v2"

"go-hep.org/x/hep/fit"
"go-hep.org/x/hep/hbook"
"go-hep.org/x/hep/hplot"
"golang.org/x/exp/rand"
"gonum.org/v1/gonum/floats"
"gonum.org/v1/gonum/optimize"
"gonum.org/v1/gonum/stat/distuv"
Expand All @@ -25,7 +25,7 @@ func ExampleH1D_gaussian() {
mean = 2.0
sigma = 4.0
// Values from gonum/optimize:
want = []float64{447.0483517081991, 2.02127773281178, 3.9965893891862687}
want = []float64{450.56454241860934, 2.0146898541006277, 3.9613086671267466}
// Values from ROOT:
// want = []float64{4.53720e+02, 1.93218e+00, 3.93188e+00}
)
Expand All @@ -36,7 +36,7 @@ func ExampleH1D_gaussian() {
dist := distuv.Normal{
Mu: mean,
Sigma: sigma,
Src: rand.New(rand.NewSource(0)),
Src: rand.New(rand.NewPCG(0, 0)),
}

// Draw some random values from the standard
Expand Down
Binary file modified fit/testdata/2d-plane-plot_golden.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified fit/testdata/h1d-gauss-plot_golden.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 17 additions & 17 deletions groot/rhist/hist_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"bytes"
"fmt"
"log"
"math/rand/v2"
"os"
"testing"

Expand All @@ -18,7 +19,6 @@ import (
"go-hep.org/x/hep/hbook"
"go-hep.org/x/hep/hbook/rootcnv"
"go-hep.org/x/hep/hbook/yodacnv"
"golang.org/x/exp/rand"
"gonum.org/v1/gonum/stat/distuv"
)

Expand All @@ -38,7 +38,7 @@ func ExampleCreate_histo1D() {
dist := distuv.Normal{
Mu: 0,
Sigma: 1,
Src: rand.New(rand.NewSource(0)),
Src: rand.New(rand.NewPCG(0, 0)),
}

// Draw some random values from the standard
Expand Down Expand Up @@ -87,12 +87,12 @@ func ExampleCreate_histo1D() {

// Output:
// original histo:
// w-mean: 0.0023919
// w-rms: 1.0628679
// w-mean: -0.0010939
// w-rms: 1.0575275
//
// histo read back:
// r-mean: 0.0023919
// r-rms: 1.0628679
// r-mean: -0.0010939
// r-rms: 1.0575275
}

func ExampleCreate_histo2D() {
Expand All @@ -111,7 +111,7 @@ func ExampleCreate_histo2D() {
dist := distuv.Normal{
Mu: 0,
Sigma: 1,
Src: rand.New(rand.NewSource(0)),
Src: rand.New(rand.NewPCG(0, 0)),
}

// Draw some random values from the standard
Expand Down Expand Up @@ -166,16 +166,16 @@ func ExampleCreate_histo2D() {

// Output:
// original histo:
// w-mean-x: +0.046442
// w-rms-x: +1.231044
// w-mean-y: -0.018977
// w-rms-y: +1.253143
// w-mean-x: +0.016810
// w-rms-x: +1.256504
// w-mean-y: +0.030289
// w-rms-y: +1.300039
//
// histo read back:
// w-mean-x: +0.046442
// w-rms-x: +1.231044
// w-mean-y: -0.018977
// w-rms-y: +1.253143
// w-mean-x: +0.016810
// w-rms-x: +1.256504
// w-mean-y: +0.030289
// w-rms-y: +1.300039
}

func TestH1(t *testing.T) {
Expand All @@ -185,7 +185,7 @@ func TestH1(t *testing.T) {
dist := distuv.Normal{
Mu: 0,
Sigma: 1,
Src: rand.New(rand.NewSource(0)),
Src: rand.New(rand.NewPCG(0, 0)),
}

// Draw some random values from the standard
Expand Down Expand Up @@ -285,7 +285,7 @@ func TestH2(t *testing.T) {
dist := distuv.Normal{
Mu: 0,
Sigma: 1,
Src: rand.New(rand.NewSource(0)),
Src: rand.New(rand.NewPCG(0, 0)),
}

// Draw some random values from the standard
Expand Down
10 changes: 5 additions & 5 deletions groot/rtree/writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package rtree
import (
"compress/flate"
"fmt"
"math/rand/v2"
"os"
"path/filepath"
"reflect"
Expand All @@ -16,7 +17,6 @@ import (
"go-hep.org/x/hep/groot/internal/rcompress"
"go-hep.org/x/hep/groot/rbase"
"go-hep.org/x/hep/groot/riofs"
"golang.org/x/exp/rand"
)

func TestFlattenArrayType(t *testing.T) {
Expand Down Expand Up @@ -143,9 +143,9 @@ func TestConcurrentWrite(t *testing.T) {
}
defer w.Close()

rng := rand.New(rand.NewSource(1234))
rng := rand.New(rand.NewPCG(1234, 1234))
for i := 0; i < 100; i++ {
evt.N = rng.Int31n(10) + 1
evt.N = rng.Int32N(10) + 1
evt.Sli = evt.Sli[:0]
for j := 0; j < int(evt.N); j++ {
evt.Sli = append(evt.Sli, rng.Float64())
Expand Down Expand Up @@ -288,9 +288,9 @@ func TestWriterWithCompression(t *testing.T) {
}
defer w.Close()

rng := rand.New(rand.NewSource(1234))
rng := rand.New(rand.NewPCG(1234, 1234))
for i := 0; i < 100; i++ {
evt.N = rng.Int31n(10) + 1
evt.N = rng.Int32N(10) + 1
evt.Sli = evt.Sli[:0]
for j := 0; j < int(evt.N); j++ {
evt.Sli = append(evt.Sli, rng.Float64())
Expand Down
20 changes: 10 additions & 10 deletions hbook/h1d_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ package hbook_test
import (
"fmt"
"math"
"math/rand/v2"

"go-hep.org/x/hep/hbook"
"golang.org/x/exp/rand"
"gonum.org/v1/gonum/stat/distuv"
)

Expand All @@ -20,7 +20,7 @@ func ExampleH1D() {
dist := distuv.Normal{
Mu: 0,
Sigma: 1,
Src: rand.New(rand.NewSource(0)),
Src: rand.New(rand.NewPCG(0, 0)),
}

// Draw some random values from the standard
Expand All @@ -34,16 +34,16 @@ func ExampleH1D() {
h.FillN([]float64{1, 2, 3}, []float64{1, 1, 1})
h.FillN([]float64{1, 2, 3}, nil) // all weights are 1.

fmt.Printf("mean: %v\n", h.XMean())
fmt.Printf("rms: %v\n", h.XRMS())
fmt.Printf("std-dev: %v\n", h.XStdDev())
fmt.Printf("std-err: %v\n", h.XStdErr())
fmt.Printf("mean: %.12f\n", h.XMean())
fmt.Printf("rms: %.12f\n", h.XRMS())
fmt.Printf("std-dev: %.12f\n", h.XStdDev())
fmt.Printf("std-err: %.12f\n", h.XStdErr())

// Output:
// mean: 0.005589967511734562
// rms: 1.0062596231244403
// std-dev: 1.0062943821322063
// std-err: 0.010059926295994191
// mean: 0.002104228518
// rms: 1.000617135827
// std-dev: 1.000664927794
// std-err: 0.010003648633
}

func ExampleAddH1D() {
Expand Down
4 changes: 2 additions & 2 deletions hbook/h2d_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ package hbook_test

import (
"log"
"math/rand/v2"

"go-hep.org/x/hep/hbook"
"golang.org/x/exp/rand"
"gonum.org/v1/gonum/mat"
"gonum.org/v1/gonum/stat/distmv"
)
Expand All @@ -21,7 +21,7 @@ func ExampleH2D() {
dist, ok := distmv.NewNormal(
[]float64{0, 1},
mat.NewSymDense(2, []float64{4, 0, 0, 2}),
rand.New(rand.NewSource(1234)),
rand.New(rand.NewPCG(1234, 1234)),
)
if !ok {
log.Fatalf("error creating distmv.Normal")
Expand Down
Loading

0 comments on commit 0d92fc6

Please sign in to comment.