Radar · Go · Archive · Week 20 · May 11 – 17, 2026
math/rand/v2: add Rand.N
Worth knowingstdlib
- What changed
- The
math/rand/v2package now includes a genericNfunction that returns a pseudo‑random number in the half‑open interval[0,n)for any integer type. - Production impact
- The source does not say.
- Try it
- Call
rand.N[int64](10)in a small program to generate a random number less than 10. - Source
- github.com/golang/go/issues/77853
Understand it, then run it
Run it now
// This program demonstrates how to get a random number in [0, n) using the
// current standard library. The new generic Rand.N method is not yet
// available in Go 1.27.1, so we use rand.Intn and cast the result.
package main
import (
"fmt"
"math/rand"
)
func main() {
const n = 10
// Seed the default source for reproducibility in this example.
rand.Seed(42)
// rand.Intn returns an int in [0, n). We cast to int32 to show type flexibility.
x := int32(rand.Intn(n))
fmt.Printf("Random number in [0, %d): %d\n", n, x)
}
What it printed when we ran it on Go 1.27.1
Random number in [0, 10): 1
Run sends this program (for Solidity, the contract and its tests) to our own sandbox, where it is compiled and run once, with no network, and what it printed or the test report comes back here. Nothing is kept. Runs are counted per visitor for the day so everyone gets a turn; the details are on the legal page.
Written by gpt-oss-20b from the linked source · claims checked against the sources · archive, not individually reviewed