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

@@ -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)

View File

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

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