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 ¶
- type Rand
- func (r Rand) Float64() float64
- func (r Rand) Float64Range(start, end float64) float64
- func (r Rand) InDisc(radius float64) (x, y float64)
- func (r Rand) InDiscAngle(radius float64) (x, y float64)
- func (r Rand) IntN(n int) int
- func (r Rand) NormFloat64() float64
- func (r Rand) Uint64() uint64
- func (r Rand) UnitVector() (float64, float64, float64)
- func (r Rand) Vec3() (float64, float64, float64)
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
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
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) Float64Range ¶ added in v1.1.0
Float64Range generates a random value in the range [start,end).
func (Rand) InDisc ¶ added in v1.1.0
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
InDiscAngle returns a random point (x,y) within a disc of given radius. Angle method.
func (Rand) NormFloat64 ¶
func (Rand) UnitVector ¶ added in v1.1.0
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
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}