randf

package module
v0.0.0-...-c4e5149 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2020 License: BSD-3-Clause Imports: 2 Imported by: 0

README

randf

A better floating-point random number generator.

This package generates uniformly-distributed pseudo-random 32-bit floating-point values in the range [0, 1], assuming that we are given an algorithm for generating pseudo-random bits. These bits should have probability 50% of being in each of two states, and be statistically independent.

The approach is to choose floating-point values in the range such that the probability that a given value is chosen is proportional to the distance between it and its two neighbors. The algorithm works in two steps, first choosing the exponent range of the value, then choosing the mantissa.

From the paper http://allendowney.com/research/rand/downey07randfloat.pdf.

Documentation

Overview

Example
package main

import (
	"fmt"

	"github.com/markcol/randf"
)

func main() {
	r := randf.New()
	r.Seed(42)
	f := r.Float32()
	fmt.Println(f)
}
Output:

0.94806355

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
}

A Rand is a sourcce of random numbers.

func New

func New() *Rand

New returns a new Rand that uses random values to generate other random values.

func (*Rand) Float32

func (r *Rand) Float32() float32

Float32 returns a random 32-bit floating-point number in the range [0.0,1.0], including 0.0, subnormals, and 1.0.

func (*Rand) Seed

func (r *Rand) Seed(v int64)

Seed uses the provided seed value to initialize the default Source to a deterministic state. If Seed is not called, the generator behaves as if seeded by Seed(1). Seed values that have the same remainder when divided by 2³¹-1 generate the same pseudo-random sequence. Seed is safe for concurrent use.

Jump to

Keyboard shortcuts

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