28 lines
579 B
Go
28 lines
579 B
Go
package main
|
|
|
|
type PseudoRNG struct {
|
|
State uint32
|
|
}
|
|
|
|
func NewRNG(seed int64) *PseudoRNG {
|
|
return &PseudoRNG{State: uint32(seed)}
|
|
}
|
|
|
|
func (r *PseudoRNG) NextFloat() float64 {
|
|
calc := (uint64(r.State)*1664525 + 1013904223) % 4294967296
|
|
r.State = uint32(calc)
|
|
return float64(r.State) / 4294967296.0
|
|
}
|
|
|
|
func (r *PseudoRNG) NextRange(min, max float64) float64 {
|
|
return min + (r.NextFloat() * (max - min))
|
|
}
|
|
|
|
func (r *PseudoRNG) PickDef(defs []ObstacleDef) *ObstacleDef {
|
|
if len(defs) == 0 {
|
|
return nil
|
|
}
|
|
idx := int(r.NextRange(0, float64(len(defs))))
|
|
return &defs[idx]
|
|
}
|