noiserand

package module
v0.0.0-...-1898cc3 Latest Latest
Warning

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

Go to latest
Published: Jun 17, 2023 License: MIT Imports: 1 Imported by: 1

README

noiserand

The noiserand package implements a random number generator that is based on multidimensional noise, using one of the dimensions as a seed, which allows slight alterations to the seed to slightly alter the generated sequence.

What is this good for?

Well, for example it can be used for procedural generation and if you have found a seed that produces an interesting result, you can explore neighboring seeds which will produce slight variations of the original output.

Usage

To use the noiserand package, first import it into your Go code:

import "github.com/Flokey82/noiserand"

Then, create a new NoiseRand object with a random seed and a noise function:

r := noiserand.New(12345.0, func(x, y float64) float64 {
    // Replace this with your own noise function.
    return 0.0
})

You can then use the Intn, Int63, and Float64 methods to generate random numbers:

// Generate a random integer in the range [0, 10).
n := r.Intn(10)

// Generate a random 63-bit integer.
n64 := r.Int63()

// Generate a random float in the range [0.0, 1.0).
f := r.Float64()

// Generate a pseudo-random permutation of the integers [0,n).
p := r.Perm(10)

Example output

package main

import (
	"log"

	"github.com/Flokey82/noiserand"
	"github.com/ojrac/opensimplex-go"
)

func main() {
    // Get a noise function that returns values in the range [0, 1).
	noise := opensimplex.NewNormalized(0)

    // Generate a random sequence of numbers using the given seed.
	seed := 12345.0
	log.Printf("rand with seed %f", seed)
	rnd := noiserand.New(seed, noise.Eval2)
	for i := 0; i < 10; i++ {
		log.Printf("%d: Float64() = %f", i, rnd.Float64())
	}

    // Slightly modify the seed and generate a new sequence of numbers.
	seed += 0.01
	log.Printf("rand with seed %f", seed)
	rnd = noiserand.New(seed, noise.Eval2)
	for i := 0; i < 10; i++ {
		log.Printf("%d: Float64() = %f", i, rnd.Float64())
	}
}

Output:

 rand with seed 12345.000000
 0: Float64() = 0.416808
 1: Float64() = 0.579680
 2: Float64() = 0.485875
 3: Float64() = 0.456083
 4: Float64() = 0.651193
 5: Float64() = 0.556411
 6: Float64() = 0.464984
 7: Float64() = 0.278727
 8: Float64() = 0.166394
 9: Float64() = 0.252161
 rand with seed 12345.010000
 0: Float64() = 0.414928
 1: Float64() = 0.581802
 2: Float64() = 0.495654
 3: Float64() = 0.462045
 4: Float64() = 0.646941
 5: Float64() = 0.550813
 6: Float64() = 0.458039
 7: Float64() = 0.282221
 8: Float64() = 0.168412
 9: Float64() = 0.253999

Note how the second sequence is very similar to the first one, but not identical. Imagine if you have a seed that generates a great procedural world, but you'd like it to be just a little bit different... that's what this package is for.

Contributions

We welcome contributions to the noiserand package! If you have ideas for improvements or new features, please feel free to submit a pull request on GitHub. We also appreciate bug reports and feedback on how the package is being used. By working together, we can make the noiserand package even better for everyone. Thank you for your support!

License

The noiserand package is licensed under the MIT License. See the LICENSE file for more information.

Documentation

Overview

Package noiserand implements a random number generator that is based on multiple dimensional noise, using one of the dimensions as a seed, which allows slight alterations to the seed to slightly alter the generated sequence.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type NoiseFunc

type NoiseFunc func(x, y float64) float64

NoiseFunc is a function that takes two float64 values and returns a float64 value.

type NoiseRand

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

NoiseRand is a random number generator that is based on multidimensional noise, using one of the dimensions as a seed, which allows slight alterations to the seed to slightly alter the generated sequence.

func New

func New(seed float64, f NoiseFunc) *NoiseRand

New creates a new NoiseRand instance with the given seed and noise function.

func (*NoiseRand) Float64

func (r *NoiseRand) Float64() float64

Float64 returns, as a float64, a pseudo-random number in [0.0,1.0).

func (*NoiseRand) Int63

func (r *NoiseRand) Int63() int64

Int63 returns a non-negative pseudo-random 63-bit integer as an int64.

func (*NoiseRand) Intn

func (r *NoiseRand) Intn(n int) int

Intn returns a random integer in the range [0, n).

func (*NoiseRand) JumpAhead

func (r *NoiseRand) JumpAhead(amount float64)

JumpAhead jumps ahead in the random number sequence by the given amount.

func (*NoiseRand) Perm

func (r *NoiseRand) Perm(n int) []int

Perm returns, as a slice of n ints, a pseudo-random permutation of the integers [0,n).

func (*NoiseRand) SetRngSeed

func (r *NoiseRand) SetRngSeed(seed int64)

SetRngSeed sets the seed used for the random number generator that is used to jump ahead in the noise sequence.

NOTE: This is NOT the seed you should rely on to alter the noise sequence.

func (*NoiseRand) SetSeed

func (r *NoiseRand) SetSeed(seed float64)

SetSeed sets the seed used for the random number generator that is used to modify the values of the random numbers.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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