Skip to content

Commit

Permalink
move from math/rand to crypto/rand
Browse files Browse the repository at this point in the history
rand.Seed has been deprecated since Go 1.20 and an alternative has been
available since Go 1.0: Programs that call Seed and then expect a
specific sequence of results from the global random source (using functions such as Int)
can be broken when a dependency changes how much it consumes from the
global random source.

To avoid such breakages, programs that need a specific result
sequence should use NewRand(NewSource(seed))
to obtain a random generator that other packages cannot access
  • Loading branch information
lostbean committed Dec 6, 2023
1 parent 00851b7 commit afec4a4
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package docker_network_allocator
import (
"context"
"fmt"
"math/rand"
"net"
"strings"
"time"
Expand Down Expand Up @@ -51,11 +50,6 @@ type DockerNetworkAllocator struct {
}

func NewDockerNetworkAllocator(dockerManager *docker_manager.DockerManager) *DockerNetworkAllocator {
// NOTE: If we need a deterministic rand seed anywhere else in the program, this will break it! The reason we do this
// here is because it's way more likely that we'll forget to seed the rand when using this class than it is that we need
// a deterministic rand seed
rand.Seed(time.Now().UnixNano())

return &DockerNetworkAllocator{
isConstructedViaConstructor: true,
dockerManager: dockerManager,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package grpc_file_streaming

import (
"bytes"
"crypto/rand"
"fmt"
"testing"

"github.com/kurtosis-tech/stacktrace"
"github.com/stretchr/testify/require"
"math/rand"
"testing"
)

const (
Expand Down
5 changes: 3 additions & 2 deletions internal_testsuites/golang/test_helpers/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@ package test_helpers

import (
"context"
"crypto/rand"
"encoding/json"
"fmt"
"github.com/kurtosis-tech/kurtosis/api/golang/core/lib/starlark_run_config"
"io"
"math/rand"
"net/http"
"os"
"path"
"strings"
"testing"
"time"

"github.com/kurtosis-tech/kurtosis/api/golang/core/lib/starlark_run_config"

"github.com/golang/protobuf/ptypes/empty"
"github.com/kurtosis-tech/example-api-server/api/golang/example_api_server_rpc_api_bindings"
"github.com/kurtosis-tech/example-api-server/api/golang/example_api_server_rpc_api_consts"
Expand Down

0 comments on commit afec4a4

Please sign in to comment.