Skip to content

Commit 2853bd2

Browse files
committed
Removed math/rand
1 parent 99cf4e3 commit 2853bd2

File tree

5 files changed

+38
-18
lines changed

5 files changed

+38
-18
lines changed

knx/cemi/cemi_test.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ package cemi
55

66
import (
77
"bytes"
8-
"math/rand"
8+
"crypto/rand"
99
"testing"
10+
11+
"github.com/mobilarte/knx-exp/knx/util"
1012
)
1113

1214
func makeRandInfoSegment() []byte {
13-
n := rand.Int() % 256
15+
n := util.Randint64() % 256
1416

1517
buffer := make([]byte, n+1)
1618
buffer[0] = byte(n)
@@ -45,7 +47,7 @@ func TestUnpack(t *testing.T) {
4547
ldataCodes := []MessageCode{LDataReqCode, LDataConCode, LDataIndCode}
4648

4749
for i := 0; i < 100; i++ {
48-
code := ldataCodes[rand.Int()%3]
50+
code := ldataCodes[util.Randint64()%3]
4951
data := append([]byte{byte(code)}, makeRandLData()...)
5052

5153
var msg Message

knx/cemi/ldata_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ package cemi
55

66
import (
77
"bytes"
8+
"crypto/rand"
89
"encoding/binary"
9-
"math/rand"
1010
"testing"
1111
)
1212

@@ -19,7 +19,7 @@ func makeRandBuffer(n int) []byte {
1919
/* unused!
2020
2121
func makeRandTPDUSegment() []byte {
22-
n := rand.Int() % 256
22+
n := util.Randint64() % 256
2323
2424
buffer := make([]byte, n+2)
2525
buffer[0] = byte(n)

knx/cemi/tpdu_test.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ package cemi
55

66
import (
77
"bytes"
8-
"math/rand"
8+
"crypto/rand"
99
"testing"
1010

1111
"github.com/mobilarte/knx-exp/knx/util"
@@ -14,10 +14,10 @@ import (
1414
func TestAppData_Pack(t *testing.T) {
1515
for i := 0; i < 100; i++ {
1616
app := AppData{
17-
Numbered: rand.Int()%2 == 0,
18-
SeqNumber: uint8(rand.Int()) % 15,
19-
Command: APCI(rand.Int()) % 15,
20-
Data: makeRandBuffer(rand.Int() % 300),
17+
Numbered: util.Randint64()%2 == 0,
18+
SeqNumber: uint8(util.Randint64()) % 15,
19+
Command: APCI(util.Randint64()) % 15,
20+
Data: makeRandBuffer(int(util.Randint64() % 300)),
2121
}
2222

2323
if len(app.Data) > 0 {
@@ -70,9 +70,9 @@ func TestAppData_Pack(t *testing.T) {
7070
func TestControlData_Pack(t *testing.T) {
7171
for i := 0; i < 100; i++ {
7272
control := ControlData{
73-
Numbered: rand.Int()%2 == 0,
74-
SeqNumber: uint8(rand.Int()) % 15,
75-
Command: uint8(rand.Int()) % 3,
73+
Numbered: util.Randint64()%2 == 0,
74+
SeqNumber: uint8(util.Randint64()) % 15,
75+
Command: uint8(util.Randint64()) % 3,
7676
}
7777

7878
data := util.AllocAndPack(&control)
@@ -107,7 +107,7 @@ func TestControlData_Pack(t *testing.T) {
107107
func TestUnpackTransportUnit(t *testing.T) {
108108
t.Run("Control", func(t *testing.T) {
109109
for i := 0; i < 100; i++ {
110-
data := []byte{0, byte(rand.Int())}
110+
data := []byte{0, byte(util.Randint64())}
111111
data[1] |= 1 << 7
112112

113113
var unit TransportUnit
@@ -145,7 +145,7 @@ func TestUnpackTransportUnit(t *testing.T) {
145145

146146
t.Run("App", func(t *testing.T) {
147147
for i := 0; i < 100; i++ {
148-
data := make([]byte, 3+rand.Int()%255)
148+
data := make([]byte, 3+util.Randint64()%255)
149149
rand.Read(data[1:])
150150

151151
data[0] = byte(len(data) - 2)

knx/knxnet/hpai_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func makeRandBuffer(size int) []byte {
5252

5353
func TestHostInfo_Unpack(t *testing.T) {
5454
for i := 0; i < 100; i++ {
55-
proto := byte(1 + (rand.Int() % 2))
55+
proto := byte(1 + (util.Randint64() % 2))
5656
data := append([]byte{8, proto}, makeRandBuffer(6)...)
5757

5858
var hi HostInfo
@@ -87,8 +87,8 @@ func TestHostInfo_Unpack(t *testing.T) {
8787
func TestHostInfo_Pack(t *testing.T) {
8888
for i := 0; i < 100; i++ {
8989
hi := HostInfo{
90-
Protocol: Protocol(1 + (rand.Int() % 2)),
91-
Port: Port(rand.Int()),
90+
Protocol: Protocol(1 + (util.Randint64() % 2)),
91+
Port: Port(util.Randint64()),
9292
}
9393
copy(hi.Address[:], makeRandBuffer(4))
9494

knx/util/rand.go

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2024 Martin Müller.
2+
// Licensed under the MIT license which can be found in the LICENSE file.
3+
4+
package util
5+
6+
import (
7+
"crypto/rand"
8+
"math"
9+
"math/big"
10+
)
11+
12+
func Randint64() int64 {
13+
val, err := rand.Int(rand.Reader, big.NewInt(int64(math.MaxInt64)))
14+
if err != nil {
15+
return 0
16+
}
17+
return val.Int64()
18+
}

0 commit comments

Comments
 (0)