A research-grade deterministic randomness laboratory for Go. This framework provides reproducible pseudo-random sequences using different algorithms, with support for replay, branching seeds, entropy analysis, and exportable streams.
- Multiple Algorithms: LCG, PCG, and Xoshiro256** generators
- Reproducibility: Deterministic sequences with seed control
- State Management: Save and restore generator states for replay
- Seed Branching: Create independent sequences from a base seed
- Entropy Analysis: Statistical analysis including Shannon entropy, chi-square, and auto-correlation
- Stream Export/Import: JSON serialization of random sequences
- High Performance: Optimized implementations with benchmarks
go get github.com/BaseMax/go-seedlabpackage main
import (
"fmt"
"github.com/BaseMax/go-seedlab"
)
func main() {
// Create a generator with a seed
gen := seedlab.NewPCG(12345)
// Generate random numbers
for i := 0; i < 5; i++ {
fmt.Println(gen.Next())
}
// Generate floats in [0.0, 1.0)
fmt.Println(gen.NextFloat64())
// Generate integers in a range [min, max)
fmt.Println(gen.NextInRange(1, 100))
}Classic PRNG using the formula: X(n+1) = (a * X(n) + c) mod m
- Fast and simple
- Uses Knuth's MMIX parameters for 64-bit
- Suitable for simulations and testing
Modern PRNG with better statistical properties
- PCG-XSH-RR variant
- Passes statistical tests
- Good balance of speed and quality
High-quality, fast PRNG
- Passes all known statistical tests
- Supports jump function for parallel streams
- Recommended for cryptographic research (non-production)
gen := seedlab.NewPCG(12345)
// Generate and save state
state := gen.GetState()
val1 := gen.Next()
// Continue generating...
for i := 0; i < 100; i++ {
gen.Next()
}
// Restore previous state
gen.SetState(state)
val2 := gen.Next() // val2 == val1baseSeed := uint64(9999)
// Create independent branches
for i := uint64(1); i <= 3; i++ {
branchSeed := seedlab.BranchSeed(baseSeed, i)
gen := seedlab.NewXoshiro256(branchSeed)
// Each branch produces a different sequence
}gen := seedlab.NewLCG(7777)
// Export stream to JSON
stream := seedlab.ExportStream(gen, 1000)
data, _ := seedlab.MarshalStream(stream)
// Import stream
stream2, _ := seedlab.UnmarshalStream(data)
gen2, _ := seedlab.ImportStream(stream2)gen := seedlab.NewXoshiro256(31415)
values := make([]uint64, 10000)
for i := 0; i < len(values); i++ {
values[i] = gen.Next()
}
metrics := seedlab.AnalyzeEntropy(values)
fmt.Printf("Shannon Entropy: %.2f bits/byte\n", metrics.ShannonEntropy)
fmt.Printf("Chi-Square: %.2f\n", metrics.ChiSquare)
fmt.Printf("Auto-correlation: %.6f\n", metrics.AutoCorrelation)cd examples/demo
go run main.gogo test -v
go test -bench=.All generators implement the Generator interface:
Next() uint64- Returns the next random uint64NextFloat64() float64- Returns a random float64 in [0.0, 1.0)NextInRange(min, max int64) int64- Returns a random int64 in [min, max)Seed(seed uint64)- Sets or resets the generator seedGetState() State- Returns the current state for replaySetState(state State) error- Restores a previous stateAlgorithm() string- Returns the algorithm name
NewLCG(seed uint64) *LCG- Create LCG generatorNewPCG(seed uint64) *PCG- Create PCG generatorNewXoshiro256(seed uint64) *Xoshiro256- Create Xoshiro256** generatorExportStream(gen Generator, n int) *Stream- Export n valuesImportStream(stream *Stream) (Generator, error)- Import streamMarshalStream(stream *Stream) ([]byte, error)- Serialize to JSONUnmarshalStream(data []byte) (*Stream, error)- Deserialize from JSONAnalyzeEntropy(values []uint64) *EntropyMetrics- Compute entropy metricsBranchSeed(originalSeed, branchID uint64) uint64- Create branch seed
- Simulations: Reproducible Monte Carlo simulations
- Testing: Deterministic test data generation
- Research: Cryptographic research (non-production)
- Benchmarking: Performance testing with controlled randomness
- Game Development: Procedural generation with save/load support
This library is intended for research, simulations, and testing. DO NOT use for production cryptographic applications. Use Go's crypto/rand for cryptographic purposes.
MIT License - see LICENSE file for details.
Contributions are welcome! Please feel free to submit issues or pull requests.