jsf

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2021 License: BlueOak-1.0.0 Imports: 0 Imported by: 1

README

jsf

This package provides an implementation of the 64-bit, 3-rotation variant of Bob Jenkins' unnamed "small noncryptographic PRNG"

For more information and the original implementation in C:
https://burtleburtle.net/bob/rand/smallprng.html

Example

jsf.JSF64 implements the math/rand.Source64 interface

package main

import (
	"fmt"
	"math/rand"
	"time"
	
	"github.com/7fffffff/jsf"
)

func main() {
	// Always seed with something, or you'll get nothing but zeroes
	src := jsf.New(time.Now().UnixNano())
	rng := rand.New(src)
	for i := 0; i < 10; i++ {
		fmt.Println(rng.Int63())
	}
}

32-bit

See the jsf32 subdirectory for an implementation of the 32-bit version. On some CPUs it might be a bit faster for generating 32-bit values, if called directly

cpu: AMD FX(tm)-6120 Six-Core Processor
pkg: github.com/7fffffff/jsf
BenchmarkUint32-6       184545068                6.793 ns/op
BenchmarkUint64-6       180301332                6.394 ns/op
pkg: github.com/7fffffff/jsf/jsf32
BenchmarkUint32-6       258255520                4.663 ns/op
BenchmarkUint64-6       100000000                10.71 ns/op
cpu: AMD Ryzen 5 2600X Six-Core Processor
pkg: github.com/7fffffff/jsf
BenchmarkUint32-12      410623514                2.946 ns/op
BenchmarkUint64-12      412275786                2.885 ns/op
pkg: github.com/7fffffff/jsf/jsf32
BenchmarkUint32-12      424884342                2.795 ns/op
BenchmarkUint64-12      291792106                4.098 ns/op

The math/rand Source and Source64 interfaces only use Int63() and Uint64(), so the 64-bit version seems preferable for that use

API Documentation

https://pkg.go.dev/github.com/7fffffff/jsf

License

This software is licensed under the Blue Oak Model License 1.0.0:
https://blueoakcouncil.org/license/1.0.0

Documentation

Overview

Package jsf provides the 64-bit, 3-rotation variant of Bob Jenkins' public-domain small noncryptographic PRNG

The original C implementation can be found here: https://burtleburtle.net/bob/rand/smallprng.html

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type JSF64

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

JSF64 implements the math/rand.Source64 interface

Callers must seed a generator through New() or Seed() before generating any numbers. An unseeded JSF64 (the zero value) will always produce a 0

func New

func New(seed int64) *JSF64

New returns a seeded generator

func (*JSF64) Int31

func (j *JSF64) Int31() int32

Int31 returns a pseudorandom value in [0, 1<<31)

func (*JSF64) Int63

func (j *JSF64) Int63() int64

Int63 returns a pseudorandom value in [0, 1<<63)

func (*JSF64) Seed

func (j *JSF64) Seed(seed int64)

Seed (re)initializes the generator

func (*JSF64) Uint32

func (j *JSF64) Uint32() uint32

Uint32 returns a pseudorandom value in [0, 1<<32)

func (*JSF64) Uint64

func (j *JSF64) Uint64() uint64

Uint64 returns a pseudorandom value in [0, 1<<64)

Directories

Path Synopsis
Package jsf32 provides the 32-bit version of Bob Jenkins' public-domain small noncryptographic PRNG The original C implementation can be found here: https://burtleburtle.net/bob/rand/smallprng.html
Package jsf32 provides the 32-bit version of Bob Jenkins' public-domain small noncryptographic PRNG The original C implementation can be found here: https://burtleburtle.net/bob/rand/smallprng.html

Jump to

Keyboard shortcuts

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