Skip to content

Instantly share code, notes, and snippets.

@enil
Created October 27, 2016 12:21
Show Gist options
  • Save enil/72547c2fe85a9ba6c03f0a021cff0147 to your computer and use it in GitHub Desktop.
Save enil/72547c2fe85a9ba6c03f0a021cff0147 to your computer and use it in GitHub Desktop.
Mock random number source
package main
import (
"fmt"
"math/rand"
)
type MockSource struct {
values []int64
position int
}
func NewMockSource(values ...int64) *MockSource {
return &MockSource{values, 0}
}
func (ms *MockSource) Int63() int64 {
value := ms.values[ms.position%len(ms.values)]
ms.position++
return value
}
func (ms MockSource) Seed(seed int64) {
}
func main() {
source := NewMockSource(1, 2, 3)
random := rand.New(source)
fmt.Println("1:", random.Int())
fmt.Println("2:", random.Int())
fmt.Println("3:", random.Int())
fmt.Println("4:", random.Int())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment