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

143 lines
4.1 KiB
V
Raw Normal View History

2020-02-03 07:00:36 +03:00
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
2019-06-23 05:21:30 +03:00
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
2019-06-22 21:20:28 +03:00
module rand
2020-06-09 16:06:07 +03:00
import rand.util
import rand.wyrand
// Configuration struct for creating a new instance of the default RNG.
pub struct PRNGConfigStruct {
seed []u32 = util.time_seed_array(2)
2019-06-22 21:20:28 +03:00
}
2020-06-09 16:06:07 +03:00
__global default_rng &wyrand.WyRandRNG
fn init() {
default_rng = new_default({})
2019-06-22 21:20:28 +03:00
}
2020-06-09 16:06:07 +03:00
// new_default returns a new instance of the default RNG. If the seed is not provided, the current time will be used to seed the instance.
pub fn new_default(config PRNGConfigStruct) &wyrand.WyRandRNG {
rng := &wyrand.WyRandRNG{}
rng.seed(config.seed)
return rng
2019-09-16 22:21:21 +03:00
}
2020-06-09 16:06:07 +03:00
// seed sets the given array of `u32` values as the seed for the `default_rng`.
pub fn seed(seed []u32) {
default_rng.seed(seed)
2020-06-01 22:13:56 +03:00
}
2020-06-09 16:06:07 +03:00
// u32() returns a uniformly distributed u32 in _[0, 2<sup>32</sup>)_
pub fn u32() u32 {
return default_rng.u32()
}
// u64() returns a uniformly distributed u64 in _[0, 2<sup>64</sup>)_
pub fn u64() u64 {
return default_rng.u64()
}
// u32n(max) returns a uniformly distributed pseudorandom 32-bit signed positive u32 in _[0, max)_
pub fn u32n(max u32) u32 {
return default_rng.u32n(max)
}
// u64n(max) returns a uniformly distributed pseudorandom 64-bit signed positive u64 in _[0, max)_
pub fn u64n(max u64) u64 {
return default_rng.u64n(max)
}
// u32_in_range(min, max) returns a uniformly distributed pseudorandom 32-bit unsigned u32 in _[min, max)_
pub fn u32_in_range(min, max u32) u32 {
return default_rng.u32_in_range(min, max)
}
// u64_in_range(min, max) returns a uniformly distributed pseudorandom 64-bit unsigned u64 in _[min, max)_
pub fn u64_in_range(min, max u64) u64 {
return default_rng.u64_in_range(min, max)
}
// int() returns a uniformly distributed pseudorandom 32-bit signed (possibly negative) int
pub fn int() int {
return default_rng.int()
}
// intn(max) returns a uniformly distributed pseudorandom 32-bit signed positive int in _[0, max)_
pub fn intn(max int) int {
return default_rng.intn(max)
}
// int_in_range(min, max) returns a uniformly distributed pseudorandom
// 32-bit signed int in [min, max). Both min and max can be negative, but we must have _min < max_.
pub fn int_in_range(min, max int) int {
return default_rng.int_in_range(min, max)
}
// int31() returns a uniformly distributed pseudorandom 31-bit signed positive int
pub fn int31() int {
return default_rng.int31()
}
// i64() returns a uniformly distributed pseudorandom 64-bit signed (possibly negative) i64
pub fn i64() i64 {
return default_rng.i64()
}
// i64n(max) returns a uniformly distributed pseudorandom 64-bit signed positive i64 in _[0, max)_
pub fn i64n(max i64) i64 {
return default_rng.i64n(max)
}
// i64_in_range(min, max) returns a uniformly distributed pseudorandom 64-bit signed int in _[min, max)_
pub fn i64_in_range(min, max i64) i64 {
return default_rng.i64_in_range(min, max)
}
// int63() returns a uniformly distributed pseudorandom 63-bit signed positive int
pub fn int63() i64 {
return default_rng.int63()
2020-06-01 22:13:56 +03:00
}
2020-06-09 16:06:07 +03:00
// f32() returns a uniformly distributed 32-bit floating point in _[0, 1)_
pub fn f32() f32 {
return default_rng.f32()
}
2020-06-09 16:06:07 +03:00
// f64() returns a uniformly distributed 64-bit floating point in _[0, 1)_
pub fn f64() f64 {
return default_rng.f64()
}
2020-06-09 16:06:07 +03:00
// f32n() returns a uniformly distributed 32-bit floating point in _[0, max)_
pub fn f32n(max f32) f32 {
return default_rng.f32n(max)
}
2020-06-09 16:06:07 +03:00
// f64n() returns a uniformly distributed 64-bit floating point in _[0, max)_
pub fn f64n(max f64) f64 {
return default_rng.f64n(max)
}
2020-06-09 16:06:07 +03:00
// f32_in_range(min, max) returns a uniformly distributed 32-bit floating point in _[min, max)_
pub fn f32_in_range(min, max f32) f32 {
return default_rng.f32_in_range(min, max)
}
2020-06-09 16:06:07 +03:00
// f64_in_range(min, max) returns a uniformly distributed 64-bit floating point in _[min, max)_
pub fn f64_in_range(min, max f64) f64 {
return default_rng.f64_in_range(min, max)
2020-06-01 22:13:56 +03:00
}
2020-07-15 22:36:06 +03:00
const (
chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
)
pub fn string(len int) string {
mut buf := malloc(len)
for i in 0..len {
buf[i] = chars[intn(chars.len)]
}
return string(buf, len)
}