rand

package module
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 2, 2025 License: Apache-2.0 Imports: 2 Imported by: 5

README

GoDoc Go Report Card CI Checks codecov

rand

Random number wrapper whose goal is to be used one instance per goroutine.

It's a wrapper over stdlib math/rand/v2 PCG - ie the fastest available in stdlib.

It provides convenience method to either have different rng each time (seed 0) or a specific repeatable sequence.

Like the underlying math/rand/v2, it is not meant for anything related to cryptography or security.

Use

go get fortio.org/rand@latest

And see the Go doc.

Benchmarks

$ go test -bench . .
goos: darwin
goarch: arm64
pkg: fortio.org/rand
cpu: Apple M3 Pro
BenchmarkSharedRand-11                  10285254               119.0 ns/op
BenchmarkPerGoRoutineRand-11            1000000000               0.7205 ns/op
BenchmarkGlobalRand-11                  1000000000               1.115 ns/op
BenchmarkPerGoRoutineChaCha8-11         1000000000               0.9193 ns/op
BenchmarkPCGUint64-11                   1000000000               0.3845 ns/op
BenchmarkChaCha8Uint64-11               1000000000               0.6418 ns/op
PASS
ok      fortio.org/rand 5.849s

Documentation

Overview

Package rand is a wrapper over stdlib math/rand/v2 PCG, the fastest available in stdlib meant to have 1 instance per goroutine for maximum performance. It provides convenience methods to either have different rng each time (seed 0) or a specific repeatable sequence. It also provides methods useful for vector graphics, ray tracing and simulations (e.g. random unit 3d vectors).

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Rand

type Rand struct {
	// contains filtered or unexported fields
}

Rand wraps a random number generator, is meant to be embedded in other structs and reused but not shared across goroutines. It's ok to copy by value as the underlying rng state is a pointer.

func New added in v1.1.0

func New(seed uint64) Rand

New generates a new Rand with the given seed. If seed is 0, a random seed is used. It's meant for the single goroutine or pre-multiple goroutine (main thread) case.

func NewIdx added in v1.1.0

func NewIdx(idx int, seed uint64) Rand

NewIdx creates a new Rand using the given index and seed. idx can be used to create different Rand instances for different goroutines. If seed is 0, a random seed is used and index is ignored.

func (Rand) Float64

func (r Rand) Float64() float64

func (Rand) Float64Range added in v1.1.0

func (r Rand) Float64Range(start, end float64) float64

Float64Range generates a random value in the range [start,end).

func (Rand) InDisc added in v1.1.0

func (r Rand) InDisc(radius float64) (x, y float64)

InDisc returns a random point (x,y) within a disc of the given radius from random source (and currently implemented via rejection sampling).

func (Rand) InDiscAngle added in v1.1.0

func (r Rand) InDiscAngle(radius float64) (x, y float64)

InDiscAngle returns a random point (x,y) within a disc of given radius. Angle method.

func (Rand) IntN

func (r Rand) IntN(n int) int

func (Rand) NormFloat64

func (r Rand) NormFloat64() float64

func (Rand) Uint64

func (r Rand) Uint64() uint64

func (Rand) UnitVector added in v1.1.0

func (r Rand) UnitVector() (float64, float64, float64)

UnitVector generates a random unit vector using normal distribution. It is the fastest of the three methods tested (versus rejection or angle methods) and produces uniformly distributed points on the unit sphere. Being both correct and most efficient, this is now the only method for generating random unit vectors provided (compared to the original tray 3 methods).

func (Rand) Vec3 added in v1.1.0

func (r Rand) Vec3() (float64, float64, float64)

Vec3 generates a random vector of 3 components in [0,1).

Example
package main

import (
	"fmt"

	"fortio.org/rand"
)

func main() {
	// Example of how it can be used with a vector type (like in fortio.org/ray).
	type Vec3 struct {
		x, y, z float64
	}

	NewVec3 := func(x, y, z float64) Vec3 {
		return Vec3{x, y, z}
	}
	RandomVec3 := func(r rand.Rand) Vec3 {
		return NewVec3(r.Vec3())
	}

	r := rand.New(42)
	v := RandomVec3(r)

	fmt.Printf("%#v", v)
}
Output:
rand_test.Vec3{x:0.7680643711325947, y:0.5560374848919416, z:0.6664016849143646}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL