rand.go 597 B

12345678910111213141516171819202122232425262728
  1. package protocol
  2. import (
  3. "math/rand"
  4. "github.com/v2ray/v2ray-core/common/protocol"
  5. )
  6. type RandomTimestampGenerator interface {
  7. Next() protocol.Timestamp
  8. }
  9. type RealRandomTimestampGenerator struct {
  10. base protocol.Timestamp
  11. delta int
  12. }
  13. func NewRandomTimestampGenerator(base protocol.Timestamp, delta int) RandomTimestampGenerator {
  14. return &RealRandomTimestampGenerator{
  15. base: base,
  16. delta: delta,
  17. }
  18. }
  19. func (this *RealRandomTimestampGenerator) Next() protocol.Timestamp {
  20. rangeInDelta := rand.Intn(this.delta*2) - this.delta
  21. return this.base + protocol.Timestamp(rangeInDelta)
  22. }