1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

rand: change rand.seed() to receive seed value

This commit is contained in:
d2verb 2019-07-14 01:17:45 +09:00 committed by Alexander Medvednikov
parent af7d49103b
commit 2cb12b4f4e
5 changed files with 41 additions and 12 deletions

View File

@ -140,7 +140,7 @@ fn main() {
} }
fn (g mut Game) init_game() { fn (g mut Game) init_game() {
rand.seed() rand.seed(time.now().uni)
g.generate_tetro() g.generate_tetro()
g.field = []array_int // TODO: g.field = [][]int g.field = []array_int // TODO: g.field = [][]int
// Generate the field, fill it with 0's, add -1's on each edge // Generate the field, fill it with 0's, add -1's on each edge

View File

@ -1,4 +1,5 @@
import rand import rand
import time
import os import os
const (HelpText = ' Usage:\t./VCasino\n 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 { fn run_wheel(bet_nbr int, bet int) int {
rand.seed() rand.seed(time.now().uni)
winning_nbr := rand.next(50) winning_nbr := rand.next(50)
print('Roulette Wheel spinning... and stops on the number $winning_nbr which is a ') print('Roulette Wheel spinning... and stops on the number $winning_nbr which is a ')
if winning_nbr % 2 == 1 { if winning_nbr % 2 == 1 {

View File

@ -1,6 +1,7 @@
import bf import bf
import rand import rand
import time
fn test_bf_new_size() { fn test_bf_new_size() {
instance := bf.new(75) instance := bf.new(75)
@ -18,7 +19,7 @@ fn test_bf_set_clear_toggle_get() {
} }
fn test_bf_and_not_or_xor() { fn test_bf_and_not_or_xor() {
rand.seed() rand.seed(time.now().uni)
len := 80 len := 80
mut input1 := bf.new(len) mut input1 := bf.new(len)
mut input2 := bf.new(len) mut input2 := bf.new(len)
@ -45,7 +46,7 @@ fn test_bf_and_not_or_xor() {
} }
fn test_clone_cmp() { fn test_clone_cmp() {
rand.seed() rand.seed(time.now().uni)
len := 80 len := 80
mut input := bf.new(len) mut input := bf.new(len)
for i := 0; i < len; i++ { for i := 0; i < len; i++ {
@ -59,7 +60,7 @@ fn test_clone_cmp() {
} }
fn test_slice_join() { fn test_slice_join() {
rand.seed() rand.seed(time.now().uni)
len := 80 len := 80
mut input := bf.new(len) mut input := bf.new(len)
for i := 0; i < len; i++ { for i := 0; i < len; i++ {
@ -82,7 +83,7 @@ fn test_slice_join() {
} }
fn test_popcount() { fn test_popcount() {
rand.seed() rand.seed(time.now().uni)
len := 80 len := 80
mut count0 := 0 mut count0 := 0
mut input := bf.new(len) mut input := bf.new(len)
@ -97,7 +98,7 @@ fn test_popcount() {
} }
fn test_hamming() { fn test_hamming() {
rand.seed() rand.seed(time.now().uni)
len := 80 len := 80
mut count := 0 mut count := 0
mut input1 := bf.new(len) mut input1 := bf.new(len)

View File

@ -4,10 +4,8 @@
module rand module rand
import time pub fn seed(s int) {
C.srand(s)
pub fn seed() {
C.srand(time.now().uni)
} }
pub fn next(max int) int { pub fn next(max int) int {
@ -15,4 +13,3 @@ pub fn next(max int) int {
} }
fn C.rand() int fn C.rand() int

30
vlib/rand/rand_test.v Normal file
View File

@ -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]
}
}