Skip to content

Commit

Permalink
Remove unnecessary calls to rand.Seed(time.Now().Unix()).
Browse files Browse the repository at this point in the history
As per https://pkg.go.dev/math/rand#Seed:
"If Seed is not called, the generator is seeded randomly at program startup."
"Prior to Go 1.20, the generator was seeded like Seed(1) at program startup. To
force the old behavior, call Seed(1) at program startup."
"As of Go 1.20 there is no reason to call Seed with a random value."

rand.Seed() is deprecated. Followup to #11015.

PiperOrigin-RevId: 685052229
  • Loading branch information
ayushr2 authored and gvisor-bot committed Oct 12, 2024
1 parent 72846ff commit a81ec22
Show file tree
Hide file tree
Showing 9 changed files with 0 additions and 33 deletions.
2 changes: 0 additions & 2 deletions pkg/compressio/compressio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,6 @@ func doTest(t harness, opts testOpts) {
var hashKey = []byte("01234567890123456789012345678901")

func TestCompress(t *testing.T) {
rand.Seed(time.Now().Unix())

var (
data = initTest(t, 10*1024*1024)
data0 = data[:0]
Expand Down
4 changes: 0 additions & 4 deletions pkg/compressio/nocompressio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,10 @@ import (
"bytes"
"fmt"
"io"
"math/rand"
"testing"
"time"
)

func TestNoCompress(t *testing.T) {
rand.Seed(time.Now().Unix())

var (
data = initTest(t, 10*1024*1024)
data0 = data[:0]
Expand Down
3 changes: 0 additions & 3 deletions pkg/state/statefile/statefile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"math/rand"
"runtime"
"testing"
"time"

"gvisor.dev/gvisor/pkg/compressio"
)
Expand All @@ -44,8 +43,6 @@ type testCase struct {
}

func TestStatefile(t *testing.T) {
rand.Seed(time.Now().Unix())

compression := map[string]CompressionLevel{
"none": CompressionLevelNone,
"compressed": CompressionLevelFlateBestSpeed,
Expand Down
4 changes: 0 additions & 4 deletions pkg/tcpip/sample/tun_tcp_connect/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,9 @@ import (
"bytes"
"fmt"
"log"
"math/rand"
"net"
"os"
"strconv"
"time"

"gvisor.dev/gvisor/pkg/rawfile"
"gvisor.dev/gvisor/pkg/tcpip"
Expand Down Expand Up @@ -100,8 +98,6 @@ func main() {
remoteAddrName := os.Args[4]
remotePortName := os.Args[5]

rand.Seed(time.Now().UnixNano())

addr := tcpip.AddrFromSlice(net.ParseIP(addrName).To4())
remote := tcpip.FullAddress{
NIC: 1,
Expand Down
4 changes: 0 additions & 4 deletions pkg/tcpip/sample/tun_tcp_echo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,10 @@ import (
"flag"
"io"
"log"
"math/rand"
"net"
"os"
"strconv"
"strings"
"time"

"gvisor.dev/gvisor/pkg/rawfile"
"gvisor.dev/gvisor/pkg/tcpip"
Expand Down Expand Up @@ -113,8 +111,6 @@ func main() {
addrName := flag.Arg(1)
portName := flag.Arg(2)

rand.Seed(time.Now().UnixNano())

// Parse the mac address.
maddr, err := net.ParseMAC(*mac)
if err != nil {
Expand Down
6 changes: 0 additions & 6 deletions runsc/boot/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"encoding/json"
"errors"
"fmt"
mrand "math/rand"
"os"
"runtime"
"strconv"
Expand Down Expand Up @@ -294,11 +293,6 @@ type FDMapping struct {
Host int
}

func init() {
// Initialize the random number generator.
mrand.Seed(gtime.Now().UnixNano())
}

// Args are the arguments for New().
type Args struct {
// Id is the sandbox ID.
Expand Down
1 change: 0 additions & 1 deletion runsc/boot/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ import (

func init() {
log.SetLevel(log.Debug)
rand.Seed(time.Now().UnixNano())
if err := fsgofer.OpenProcSelfFD("/proc/self/fd"); err != nil {
panic(err)
}
Expand Down
4 changes: 0 additions & 4 deletions test/benchmarks/tcp/tcp_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,10 +398,6 @@ func main() {
if *forward == "" {
log.Fatalf("no forward provided")
}
// Seed the random number generator to ensure that we are given MAC addresses that don't
// for the case of the client and server stack.
rand.Seed(time.Now().UTC().UnixNano())

if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
Expand Down
5 changes: 0 additions & 5 deletions test/fsstress/fsstress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,12 @@ import (
"strconv"
"strings"
"testing"
"time"

"github.com/docker/docker/api/types/mount"
"gvisor.dev/gvisor/pkg/test/dockerutil"
"gvisor.dev/gvisor/pkg/test/testutil"
)

func init() {
rand.Seed(int64(time.Now().Nanosecond()))
}

func TestMain(m *testing.M) {
dockerutil.EnsureSupportedDockerVersion()
flag.Parse()
Expand Down

0 comments on commit a81ec22

Please sign in to comment.