Skip to content

Commit 54f1fb2

Browse files
committed
Replace crypto/rand to math/rand/v2
1 parent 8b053f3 commit 54f1fb2

File tree

7 files changed

+83
-143
lines changed

7 files changed

+83
-143
lines changed

math/rand/const.go

Lines changed: 0 additions & 13 deletions
This file was deleted.

math/rand/const_test.go

Lines changed: 0 additions & 39 deletions
This file was deleted.

math/rand/crypto.go

Lines changed: 0 additions & 33 deletions
This file was deleted.

math/rand/crypto_test.go

Lines changed: 0 additions & 56 deletions
This file was deleted.

math/rand/math.go

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,34 @@
11
package rand
22

3-
import "math/rand/v2"
3+
import (
4+
"math/rand/v2"
5+
6+
"github.com/itsubaki/q/math/number"
7+
)
48

59
var Float64 = rand.Float64
10+
11+
// Const returns a constant number in [0.0, 1.0).
12+
func Const(seed ...uint64) func() float64 {
13+
var s0, s1 uint64
14+
if len(seed) > 0 {
15+
s0 = seed[0]
16+
}
17+
18+
if len(seed) > 1 {
19+
s1 = seed[1]
20+
}
21+
22+
return rand.New(rand.NewPCG(s0, s1)).Float64
23+
}
24+
25+
// Coprime returns a random coprime number in [2, N).
26+
func Coprime(N int) int {
27+
min, max := 2, N-2
28+
for {
29+
a := rand.N(max-1) + min
30+
if number.GCD(N, a) == 1 {
31+
return a
32+
}
33+
}
34+
}

math/rand/math_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,54 @@
11
package rand_test
22

33
import (
4+
"fmt"
45
"testing"
56

67
"github.com/itsubaki/q/math/rand"
78
)
89

10+
func ExampleCoprime() {
11+
p := rand.Coprime(15)
12+
13+
for _, e := range []int{2, 4, 7, 8, 11, 13, 14} {
14+
if p == e {
15+
fmt.Println("found")
16+
break
17+
}
18+
}
19+
20+
// Output:
21+
// found
22+
}
23+
24+
func ExampleConst() {
25+
c := rand.Const()
26+
fmt.Printf("%.13f\n", c())
27+
fmt.Printf("%.13f\n", c())
28+
fmt.Printf("%.13f\n", c())
29+
fmt.Printf("%.13f\n", rand.Const(1)())
30+
fmt.Printf("%.13f\n", rand.Const(1)())
31+
fmt.Printf("%.13f\n", rand.Const(1)())
32+
fmt.Printf("%.13f\n", rand.Const(2)())
33+
fmt.Printf("%.13f\n", rand.Const(3)())
34+
fmt.Printf("%.13f\n", rand.Const(1, 0)())
35+
fmt.Printf("%.13f\n", rand.Const(1, 1)())
36+
fmt.Printf("%.13f\n", rand.Const(1, 2)())
37+
38+
// Output:
39+
// 0.9999275824803
40+
// 0.8856419373529
41+
// 0.3814775277115
42+
// 0.2384231908739
43+
// 0.2384231908739
44+
// 0.2384231908739
45+
// 0.8269781200925
46+
// 0.8353847703964
47+
// 0.2384231908739
48+
// 0.3402859786606
49+
// 0.6764556596678
50+
}
51+
952
func TestFloat64(t *testing.T) {
1053
r := rand.Float64()
1154
if r >= 0 && r < 1 {
@@ -14,3 +57,12 @@ func TestFloat64(t *testing.T) {
1457

1558
t.Fail()
1659
}
60+
61+
func TestConst(t *testing.T) {
62+
r := rand.Const()()
63+
if r >= 0 && r < 1 {
64+
return
65+
}
66+
67+
t.Fail()
68+
}

quantum/qubit/qubit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ type Qubit struct {
2525
func New(z ...complex128) *Qubit {
2626
q := &Qubit{
2727
vector: vector.New(z...),
28-
Rand: rand.Crypto(),
28+
Rand: rand.Float64,
2929
}
3030

3131
q.Normalize()

0 commit comments

Comments
 (0)