Created
October 27, 2016 12:21
-
-
Save enil/72547c2fe85a9ba6c03f0a021cff0147 to your computer and use it in GitHub Desktop.
Mock random number source
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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