V: rand.seed() not generating unique randomness

Created on 2 Jul 2019  路  4Comments  路  Source: vlang/v

V version: V 0.1.11 with latest commits
OS: Linux/Manjaro with 4.19 kernel

What did you do?

import rand
fn main() {
rand.seed()

for i := 0; i < 5; i++ {
    mut j := rand.next(10)
    println('$i for $j')
}

for i := 0; i < 5; i++ {
    mut b := rand.next(10)
    println('$i for $b')
}
}

What did you expect to see?
A different set of numbers each time I ran it.

What did you see instead?
The set of numbers were always the same.

Bug

All 4 comments

Can you post the generated C code?

time.now().uni always returns 0.

import time

n := time.now()
println(n)

C.srand(time.now().uni)
println(C.rand())

C.srand(0)
mut r := C.rand()
println('0:$r')

C.srand(1)
r = C.rand()
println('0:$r')

C.srand(2)
r = C.rand()
println('0:$r')
{ year: 2019, month: 7, day: 3, hour: 7, minute: 47, second: 52, uni: 0 }
1804289383
0:1804289383
0:1804289383
0:1505335290
============ running a ============

https://github.com/vlang/v/blob/5c42a31b9bd19b92ed433f06a90ea139aa99f923/vlib/rand/rand.v#L9-L11

This issue was just fixed. Could you pull and run your code again to confirm.

Just tested again, works perfectly now.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

markgraydev picture markgraydev  路  3Comments

shouji-kazuo picture shouji-kazuo  路  3Comments

vtereshkov picture vtereshkov  路  3Comments

PavelVozenilek picture PavelVozenilek  路  3Comments

clpo13 picture clpo13  路  3Comments