pgen

package module
v0.0.8 Latest Latest
Warning

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

Go to latest
Published: Mar 14, 2024 License: Unlicense Imports: 1 Imported by: 0

README

pgen

Build Status Go Report Card go.dev reference

Overview

A Go package for deterministic generation of random-like numbers.

This package is designed for simulations and procedural generation where you need to generate random-like features from a known seed.

A standard number generator produces random numbers in a sequence so if a code refactor changes the order in which you call the rng you'll get a different result. pgen allows you to specify which number in the random sequence is to be used for a specific procedurally generated feature. This preserves behaviour even if code refactoring changes the order in which the simulation is generated.

Installation

Simply run the following in your module:

go get github.com/iand/pgen

Documentation is at http://godoc.org/github.com/iand/pgen

Usage

Example of a procedural level generator:

package main

import (
    "github.com/iand/pgen"
    "math/rand"
)

// generateLevel generates a new game level based on the level seed. It will always
// generate the same level no matter how many other levels were generated before
// or if the order of generation is changed.
func generateLevel(levelSeed int64) {
    // These constants control which random number is returned by the generator
    const (
        numberOfPits = iota
        numberOfEnemies
        spawnPointX
        spawnPointY
        legendaryLootChance
    )
    
    gen := pgen.New(levelSeed)

    // Generate up to 8 pits
    for i := 0; i < gen.Intn(numberOfPits, 8) {
        generatePit()
    }

    // Generate up to 4 enemies
    for i := 0; i < gen.Intn(numberOfEnemies, 4) {
        generateEnemy()
    }

    // Starting location on the 64x64 grid
    x, y := gen.Intn(spawnPointX,64), gen.Intn(spawnPointY,64)
    placePlayer(x, y)

    // Is there a legendary item?
    if rand.Float64() <= gen.Intn(legendaryLootChance).Float64() {
        placeLoot()
    }
}       

License

This is free and unencumbered software released into the public domain. For more information, see http://unlicense.org/ or the accompanying UNLICENSE file.

Documentation

Overview

Package pgen is a package for deterministic generation of random-like numbers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Gen

type Gen int64

A Gen is a deterministic generator of random-like numbers.

func New

func New(i int64) Gen

func (Gen) Float32

func (g Gen) Float32(i int64) float32

Float32 generates the random number for index i and converts it to a float32 in the range [0,1).

func (Gen) Float64

func (g Gen) Float64(i int64) float64

Float64 generates the random number for index i and converts it to a float64 in the range [0,1).

func (Gen) Int32

func (g Gen) Int32(i int64) int32

Int32 generates the random number for index i. It returns integers in the range [0,2^31-1].

func (Gen) Int64

func (g Gen) Int64(i int64) int64

Int64 generates the random number for index i. It returns integers in the range [0,2^63-1].

func (Gen) Int64n added in v0.0.3

func (g Gen) Int64n(i int64, n int64) int64

Int64n generates the random number for index i using 64 bits. It returns integers in the range [0,n).

func (Gen) Intn

func (g Gen) Intn(i int64, n int) int

Intn generates the random number for index i. It returns integers in the range [0,n).

func (Gen) Rand

func (g Gen) Rand(i int64) *rand.Rand

Rand returns a random number generator for index i.

func (Gen) Uint64

func (g Gen) Uint64(i int64) uint64

Uint64 generates the random number for index i. It returns integers in the range [0,2^64-1].

Jump to

Keyboard shortcuts

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