mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
rand: add missing rand.u16(), update doc comments, add test
This commit is contained in:
parent
3a09142ace
commit
c382f4d310
@ -505,6 +505,16 @@ pub fn seed(seed []u32) {
|
|||||||
default_rng.seed(seed)
|
default_rng.seed(seed)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// u8 returns a uniformly distributed pseudorandom 8-bit unsigned positive `u8`.
|
||||||
|
pub fn u8() u8 {
|
||||||
|
return default_rng.u8()
|
||||||
|
}
|
||||||
|
|
||||||
|
// u16 returns a uniformly distributed pseudorandom 16-bit unsigned positive `u16`.
|
||||||
|
pub fn u16() u16 {
|
||||||
|
return default_rng.u16()
|
||||||
|
}
|
||||||
|
|
||||||
// u32 returns a uniformly distributed `u32` in range `[0, 2³²)`.
|
// u32 returns a uniformly distributed `u32` in range `[0, 2³²)`.
|
||||||
pub fn u32() u32 {
|
pub fn u32() u32 {
|
||||||
return default_rng.u32()
|
return default_rng.u32()
|
||||||
@ -550,11 +560,6 @@ pub fn intn(max int) !int {
|
|||||||
return default_rng.intn(max)
|
return default_rng.intn(max)
|
||||||
}
|
}
|
||||||
|
|
||||||
// byte returns a uniformly distributed pseudorandom 8-bit unsigned positive `byte`.
|
|
||||||
pub fn u8() u8 {
|
|
||||||
return default_rng.u8()
|
|
||||||
}
|
|
||||||
|
|
||||||
// int_in_range returns a uniformly distributed pseudorandom 32-bit signed int in range `[min, max)`.
|
// int_in_range returns a uniformly distributed pseudorandom 32-bit signed int in range `[min, max)`.
|
||||||
// Both `min` and `max` can be negative, but we must have `min < max`.
|
// Both `min` and `max` can be negative, but we must have `min < max`.
|
||||||
pub fn int_in_range(min int, max int) !int {
|
pub fn int_in_range(min int, max int) !int {
|
||||||
|
@ -192,6 +192,31 @@ fn test_rand_u8() {
|
|||||||
assert all[0] != all[128]
|
assert all[0] != all[128]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn test_rand_u16() {
|
||||||
|
mut all := []u16{}
|
||||||
|
mut same_as_previous := 0
|
||||||
|
mut previous := u16(0)
|
||||||
|
for _ in 0 .. 65536 {
|
||||||
|
x := rand.u16()
|
||||||
|
assert x >= 0
|
||||||
|
assert x <= 65535
|
||||||
|
all << x
|
||||||
|
if previous == x {
|
||||||
|
same_as_previous++
|
||||||
|
// dump(previous)
|
||||||
|
// dump(x)
|
||||||
|
}
|
||||||
|
previous = x
|
||||||
|
}
|
||||||
|
assert same_as_previous < 1000
|
||||||
|
all.sort(a < b)
|
||||||
|
assert all[0] != all[65535]
|
||||||
|
assert all[0] != all[32768]
|
||||||
|
// dump( all[0] )
|
||||||
|
// dump( all[65535] )
|
||||||
|
// dump( all[32768] )
|
||||||
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
string_count = 25
|
string_count = 25
|
||||||
)
|
)
|
||||||
|
Loading…
Reference in New Issue
Block a user