This site is being rebuilt and some pages are out of date. For current details, write to [email protected]. This notice goes away when the rebuild is done.

No analytics unless you allow it, no tracking. This site keeps in your browser the language you pick, the theme, its colour, which site you chose, the currency on the pricing page and that you closed this notice; signing in adds session cookies. The legal page has the details.

Sign in

Radar · Go · Archive · Week 20 · May 11 – 17, 2026

math/rand/v2: add Rand.N

Worth knowingstdlib

What changed
The math/rand/v2 package now includes a generic N function 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

Todaygo
// 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