ID/generator.go

26 lines
370 B
Go
Raw Normal View History

2016-09-18 05:55:42 +02:00
package ID
import "math/rand"
const MaxID = 2 << 24
type Type uint
type Generator struct {
// IDEA: i hate you
step Type
Last Type
}
func (idg *Generator) Next() Type {
2016-09-18 05:55:42 +02:00
idg.Last = (idg.Last + idg.step) % MaxID
return idg.Last
}
func New(seed uint32) *Generator {
return &Generator{
2016-09-18 05:55:42 +02:00
Last: Type(seed % MaxID),
step: Type((rand.Uint32() % MaxID) | 1),
}
}