diff --git a/examples/tetris/tetris.v b/examples/tetris/tetris.v index 7cd6a350b0..d51fac8d2f 100644 --- a/examples/tetris/tetris.v +++ b/examples/tetris/tetris.v @@ -140,7 +140,7 @@ fn main() { } fn (g mut Game) init_game() { - rand.seed() + rand.seed(time.now().uni) g.generate_tetro() g.field = []array_int // TODO: g.field = [][]int // Generate the field, fill it with 0's, add -1's on each edge diff --git a/examples/vcasino/VCasino.v b/examples/vcasino/VCasino.v index 1698ee1a06..970ec18394 100644 --- a/examples/vcasino/VCasino.v +++ b/examples/vcasino/VCasino.v @@ -1,4 +1,5 @@ import rand +import time import os const (HelpText = ' Usage:\t./VCasino\n @@ -87,7 +88,7 @@ fn get_bet(money int) int { } fn run_wheel(bet_nbr int, bet int) int { - rand.seed() + rand.seed(time.now().uni) winning_nbr := rand.next(50) print('Roulette Wheel spinning... and stops on the number $winning_nbr which is a ') if winning_nbr % 2 == 1 { diff --git a/vlib/bf/bf_test.v b/vlib/bf/bf_test.v index ef8ef5b87f..ab39dfb815 100644 --- a/vlib/bf/bf_test.v +++ b/vlib/bf/bf_test.v @@ -1,6 +1,7 @@ import bf import rand +import time fn test_bf_new_size() { instance := bf.new(75) @@ -18,7 +19,7 @@ fn test_bf_set_clear_toggle_get() { } fn test_bf_and_not_or_xor() { - rand.seed() + rand.seed(time.now().uni) len := 80 mut input1 := bf.new(len) mut input2 := bf.new(len) @@ -45,7 +46,7 @@ fn test_bf_and_not_or_xor() { } fn test_clone_cmp() { - rand.seed() + rand.seed(time.now().uni) len := 80 mut input := bf.new(len) for i := 0; i < len; i++ { @@ -59,7 +60,7 @@ fn test_clone_cmp() { } fn test_slice_join() { - rand.seed() + rand.seed(time.now().uni) len := 80 mut input := bf.new(len) for i := 0; i < len; i++ { @@ -82,7 +83,7 @@ fn test_slice_join() { } fn test_popcount() { - rand.seed() + rand.seed(time.now().uni) len := 80 mut count0 := 0 mut input := bf.new(len) @@ -97,7 +98,7 @@ fn test_popcount() { } fn test_hamming() { - rand.seed() + rand.seed(time.now().uni) len := 80 mut count := 0 mut input1 := bf.new(len) diff --git a/vlib/rand/rand.v b/vlib/rand/rand.v index e6b8efa2a5..8df348dc32 100644 --- a/vlib/rand/rand.v +++ b/vlib/rand/rand.v @@ -4,10 +4,8 @@ module rand -import time - -pub fn seed() { - C.srand(time.now().uni) +pub fn seed(s int) { + C.srand(s) } pub fn next(max int) int { @@ -15,4 +13,3 @@ pub fn next(max int) int { } fn C.rand() int - diff --git a/vlib/rand/rand_test.v b/vlib/rand/rand_test.v new file mode 100644 index 0000000000..737dfbb282 --- /dev/null +++ b/vlib/rand/rand_test.v @@ -0,0 +1,30 @@ +import rand + +fn gen_randoms(seed int) []int { + mut randoms := [0; 20] + rand.seed(seed) + for i in 0..20 { + randoms[i] = rand.next(100) + } + return randoms +} + +fn test_rand_reproducibility() { + mut randoms1 := gen_randoms(42) + mut randoms2 := gen_randoms(42) + assert randoms1.len == randoms2.len + + mut len := randoms1.len + for i in 0..len { + assert randoms1[i] == randoms2[i] + } + + randoms1 = gen_randoms(256) + randoms2 = gen_randoms(256) + assert randoms1.len == randoms2.len + + len = randoms1.len + for i in 0..len { + assert randoms1[i] == randoms2[i] + } +}