# Fix "rand.Seed has been deprecated since Go 1.20" (SA1019) in a Go Program

Yes. An agent removes or replaces each rand.Seed call by what it was for, aligns go.mod and CI so the result stays random, and opens a pull request.

[Run this on Rush](https://cloud.getrush.ai/run/fix-rand-seed-deprecated/math-rand/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

Since Go 1.20 the global generator in `math/rand` seeds itself at startup, so the line most Go programs begin with, `rand.Seed(time.Now().UnixNano())`, does nothing useful and is marked deprecated. Linters report it as SA1019. Deleting the line is correct only if every toolchain that builds the program is 1.20 or newer, and that is a fact about the repository, not about the line. brianstrauch/solitaire-tui has the call at `main.go:13`, and its deck is shuffled in `pkg/deck.go` with the package-level `rand.Shuffle`. Its `go.mod` says `go 1.19`, and `.github/workflows/go.yml` installs Go with `go-version: 1.19`. On Go 1.19 an unseeded generator starts from the same state on every run. Remove the seed there and every game deals the same cards.

The agent reads those three files together and treats them as one change. It also explains why the warning may never have appeared. staticcheck reads the `go` line of `go.mod` to decide which deprecations apply: with `go 1.19` it prints nothing for this repository, and with `go 1.20` it prints the SA1019 finding at `main.go:13:2`. A team usually meets the message the day someone raises the Go version, in a pull request that seemed unrelated.

So the agent raises the `go` directive to a version that seeds automatically, sets the workflow's `go-version` to match, and deletes the `rand.Seed` call along with the `time` and `math/rand` imports that only it used in that file. It runs `go vet ./...`, staticcheck and `go test ./...`. The tests in `pkg/deck_test.go` and `pkg/card_test.go` build and shuffle decks without assuming an order, so they pass unchanged. To show the shuffle still varies, it prints a permutation from two separate processes and includes both lines.

The other kind of call needs the opposite treatment. A program that calls `rand.Seed(42)` wants the same sequence each time, for a simulation or a test. From Go 1.24, once `go.mod` says 1.24 or later, that call is ignored and the sequence changes on every run without any error. With `go 1.23` in `go.mod` the same toolchain still honours the seed. For those call sites the agent creates a local generator, `rand.New(rand.NewSource(seed))`, and passes it to the code that needs repeatable numbers. solitaire-tui has none, and the agent says it looked.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Fix the deprecated rand.Seed calls in this Go project (staticcheck SA1019: \"rand.Seed has been deprecated since Go 1.20\"). Find every call to math/rand's Seed, including in tests and example code, and classify each one: seeded from the clock or another changing value, which only wanted randomness, or seeded from a constant or a flag, which wanted a repeatable sequence. Read go.mod's go directive, any toolchain line, and every place a Go version is pinned: CI workflows, Dockerfiles, Makefiles and version files. For clock-seeded calls, remove the call only if every build uses Go 1.20 or later; raise the go directive and the pinned versions together so that is true, and say what you raised. For constant-seeded calls, replace the global generator with a local one created by rand.New(rand.NewSource(seed)) and pass it to the functions that use it; do not leave them on the global functions, because Go 1.24 and later ignore Seed. Remove imports that become unused. Do not switch to math/rand/v2 or crypto/rand unless the code generates secrets with math/rand, in which case report that as a separate finding. Run go vet, staticcheck and the tests. Show that unseeded output differs between two separate runs, and that any seeded generator gives the same output twice. Open a pull request.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

For solitaire-tui the pull request touches three files: `main.go` loses the seed line and two imports, `go.mod` gets a newer `go` directive, and `.github/workflows/go.yml` gets the matching `go-version`. The description puts the reasoning before the diff: which toolchains built the project before, what an unseeded generator does on the oldest of them, and why the version bump belongs in the same commit as the deletion.

It includes the linter output for both states of `go.mod`, so the reviewer sees the finding appear with the newer directive and disappear after the fix, and the two permutations printed by separate processes. Where a project has constant seeds, the description lists each converted call site and shows the same sequence printed twice from the local generator.

The reviewer checks one thing the agent cannot see from the repository: whether anything else builds this code with an older Go, such as a packaging script or a downstream Dockerfile kept in another repository. If so, the seed line has to stay until that build moves.

## Run it on every failed lint check

The finding tends to arrive as a red check on a pull request that bumps the Go version. Save the prompt above as an automation on the repository's Project with a `github` trigger on `check_suite.failure`. The failed check is placed after the saved prompt as event data, and the prompt itself tells the agent what to fix, so nothing has to be filled in. Only checks on work by a repository owner, member or collaborator fire it, and bot senders and forks never do.

A check suite can fail for many reasons, and this prompt handles one of them. Tell the agent in the saved prompt to stop and report when the failure is not SA1019 for `rand.Seed`, and set the automation's max runs per hour to a small number so a noisy afternoon of failing builds does not start a run for each.

## Why run it on Rush?

The correct edit depends on which Go builds the code, and the way to know is to build it. In its own sandbox the agent can change the `go` directive, run the linter, change it back and run it again, and run the program in two processes to compare output. Those are quick experiments that people skip, and they are the difference between deleting a dead line and shipping a card game that deals one hand forever.

Everything it runs is kept with the session: the staticcheck message, the vet and test output, the two permutations. A reviewer who doubts that Go 1.24 really ignores a constant seed can read the commands and the two differing lines instead of looking for the release note.

## Frequently asked questions

### Do I still need to call rand.Seed?

Not on Go 1.20 or later. The global generator is seeded randomly when the program starts, so the top-level functions such as `rand.Intn` and `rand.Shuffle` give different results on each run with no setup. On Go 1.19 and earlier you do need it, or every run produces the same sequence.

### How do I get a reproducible sequence now?

Create your own generator with `rand.New(rand.NewSource(seed))` and call its methods instead of the package-level functions. That works on every Go version. Do not rely on `rand.Seed` with a constant: once `go.mod` declares Go 1.24 or later, the call has no effect.

### Why did the warning only appear after I changed go.mod?

staticcheck applies a deprecation only when the module's declared Go version is at least the version that deprecated the API. With `go 1.19` in `go.mod` it stays quiet about `rand.Seed`, and with `go 1.20` it reports SA1019. The code did not change, the declared version did.

### Should I move to math/rand/v2?

It is a reasonable later step and a larger one: v2 has no `Seed` at all, renames several functions and needs Go 1.22. The agent keeps this pull request to the deprecation and notes v2 as an option. If the code uses `math/rand` for tokens or passwords, that is a security finding, and the answer is `crypto/rand`.

## More jobs like this: Fix a bug from an alert

- [Fix "TypeError: Random.shuffle() takes 2 positional arguments but 3 were given" on Python 3.11](https://getrush.ai/run/fix-random-shuffle-takes-2-positional-arguments/random/)
- [Fix "ReactDOM.render is no longer supported in React 18. Use createRoot instead"](https://getrush.ai/run/fix-reactdom-render-no-longer-supported/react/)
- [Fix "TypeError: read_csv() got an unexpected keyword argument 'error_bad_lines'" in pandas 2](https://getrush.ai/run/fix-read-csv-error-bad-lines-type-error/pandas/)
- [Fix "Error: req#logout requires a callback function" in Passport 0.6 and later](https://getrush.ai/run/fix-req-logout-requires-a-callback-function/passport/)
- [Fix "LookupError: Resource punkt_tab not found" in word_tokenize](https://getrush.ai/run/fix-resource-punkt-tab-not-found/nltk/)
- [Fix "ImportError: cannot import name 'SafeConfigParser' from 'configparser'" on Python 3.12](https://getrush.ai/run/fix-safeconfigparser-import-error/configparser/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
