2021-01-18 15:20:06 +03:00
|
|
|
// Copyright (c) 2019-2021 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
|
|
|
|
|
2021-01-26 16:55:09 +03:00
|
|
|
import rand.seed
|
2020-06-09 16:06:07 +03:00
|
|
|
import rand.wyrand
|
2020-07-26 13:10:56 +03:00
|
|
|
import time
|
|
|
|
|
2020-12-27 21:06:17 +03:00
|
|
|
// PRNGConfigStruct is a configuration struct for creating a new instance of the default RNG.
|
2021-03-03 14:41:00 +03:00
|
|
|
// Note that the RNGs may have a different number of u32s required for seeding. The default
|
|
|
|
// generator WyRand used 64 bits, ie. 2 u32s so that is the default. In case your desired generator
|
|
|
|
// uses a different number of u32s, use the `seed.time_seed_array()` method with the correct
|
|
|
|
// number of u32s.
|
2020-06-09 16:06:07 +03:00
|
|
|
pub struct PRNGConfigStruct {
|
2021-01-26 16:55:09 +03:00
|
|
|
seed []u32 = seed.time_seed_array(2)
|
2019-06-22 21:20:28 +03:00
|
|
|
}
|
|
|
|
|
2021-03-03 14:41:00 +03:00
|
|
|
// PRNG is a common interface for all PRNGs that can be used seamlessly with the rand
|
|
|
|
// modules's API. It defines all the methods that a PRNG (in the vlib or custom made) must
|
|
|
|
// implement in order to ensure that _all_ functions can be used with the generator.
|
|
|
|
pub interface PRNG {
|
|
|
|
seed(seed_data []u32)
|
|
|
|
u32() u32
|
|
|
|
u64() u64
|
|
|
|
u32n(max u32) u32
|
|
|
|
u64n(max u64) u64
|
|
|
|
u32_in_range(min u32, max u32) u32
|
|
|
|
u64_in_range(min u64, max u64) u64
|
|
|
|
int() int
|
|
|
|
i64() i64
|
|
|
|
int31() int
|
|
|
|
int63() i64
|
|
|
|
intn(max int) int
|
|
|
|
i64n(max i64) i64
|
|
|
|
int_in_range(min int, max int) int
|
|
|
|
i64_in_range(min i64, max i64) i64
|
|
|
|
f32() f32
|
|
|
|
f64() f64
|
|
|
|
f32n(max f32) f32
|
|
|
|
f64n(max f64) f64
|
|
|
|
f32_in_range(min f32, max f32) f32
|
|
|
|
f64_in_range(min f64, max f64) f64
|
|
|
|
}
|
|
|
|
|
2021-05-08 13:32:29 +03:00
|
|
|
__global (
|
|
|
|
default_rng &PRNG
|
|
|
|
)
|
2021-03-03 09:36:56 +03:00
|
|
|
|
2020-12-27 21:06:17 +03:00
|
|
|
// init initializes the default RNG.
|
2020-06-09 16:06:07 +03:00
|
|
|
fn init() {
|
2021-03-03 09:36:56 +03:00
|
|
|
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.
|
2021-03-03 14:41:00 +03:00
|
|
|
pub fn new_default(config PRNGConfigStruct) &PRNG {
|
2020-07-24 02:55:55 +03:00
|
|
|
mut rng := &wyrand.WyRandRNG{}
|
2020-06-09 16:06:07 +03:00
|
|
|
rng.seed(config.seed)
|
|
|
|
return rng
|
2019-09-16 22:21:21 +03:00
|
|
|
}
|
2020-05-27 16:41:37 +03:00
|
|
|
|
2021-03-03 14:41:00 +03:00
|
|
|
// get_current_rng returns the PRNG instance currently in use. If it is not changed, it will be an instance of wyrand.WyRandRNG.
|
|
|
|
pub fn get_current_rng() &PRNG {
|
|
|
|
return default_rng
|
|
|
|
}
|
|
|
|
|
|
|
|
// set_rng changes the default RNG from wyrand.WyRandRNG (or whatever the last RNG was) to the one
|
2021-05-08 13:32:29 +03:00
|
|
|
// provided by the user. Note that this new RNG must be seeded manually with a constant seed or the
|
2021-03-03 14:41:00 +03:00
|
|
|
// `seed.time_seed_array()` method. Also, it is recommended to store the old RNG in a variable and
|
|
|
|
// should be restored if work with the custom RNG is complete. It is not necessary to restore if the
|
|
|
|
// program terminates soon afterwards.
|
|
|
|
pub fn set_rng(rng &PRNG) {
|
2021-05-07 15:58:48 +03:00
|
|
|
default_rng = unsafe { rng }
|
2021-03-03 14:41:00 +03:00
|
|
|
}
|
|
|
|
|
2021-03-12 22:24:43 +03:00
|
|
|
// seed sets the given array of `u32` values as the seed for the `default_rng`. The default_rng is
|
|
|
|
// an instance of WyRandRNG which takes 2 u32 values. When using a custom RNG, make sure to use
|
|
|
|
// the correct number of u32s.
|
2020-06-09 16:06:07 +03:00
|
|
|
pub fn seed(seed []u32) {
|
2021-03-03 09:36:56 +03:00
|
|
|
default_rng.seed(seed)
|
2020-06-01 22:13:56 +03:00
|
|
|
}
|
|
|
|
|
2020-12-27 21:06:17 +03:00
|
|
|
// u32 returns a uniformly distributed `u32` in range `[0, 2³²)`.
|
2020-06-09 16:06:07 +03:00
|
|
|
pub fn u32() u32 {
|
2021-03-03 09:36:56 +03:00
|
|
|
return default_rng.u32()
|
2020-06-09 16:06:07 +03:00
|
|
|
}
|
|
|
|
|
2020-12-27 21:06:17 +03:00
|
|
|
// u64 returns a uniformly distributed `u64` in range `[0, 2⁶⁴)`.
|
2020-06-09 16:06:07 +03:00
|
|
|
pub fn u64() u64 {
|
2021-03-03 09:36:56 +03:00
|
|
|
return default_rng.u64()
|
2020-06-09 16:06:07 +03:00
|
|
|
}
|
|
|
|
|
2020-12-27 21:06:17 +03:00
|
|
|
// u32n returns a uniformly distributed pseudorandom 32-bit signed positive `u32` in range `[0, max)`.
|
2020-06-09 16:06:07 +03:00
|
|
|
pub fn u32n(max u32) u32 {
|
2021-03-03 09:36:56 +03:00
|
|
|
return default_rng.u32n(max)
|
2020-06-09 16:06:07 +03:00
|
|
|
}
|
|
|
|
|
2020-12-27 21:06:17 +03:00
|
|
|
// u64n returns a uniformly distributed pseudorandom 64-bit signed positive `u64` in range `[0, max)`.
|
2020-06-09 16:06:07 +03:00
|
|
|
pub fn u64n(max u64) u64 {
|
2021-03-03 09:36:56 +03:00
|
|
|
return default_rng.u64n(max)
|
2020-06-09 16:06:07 +03:00
|
|
|
}
|
|
|
|
|
2020-12-27 21:06:17 +03:00
|
|
|
// u32_in_range returns a uniformly distributed pseudorandom 32-bit unsigned `u32` in range `[min, max)`.
|
2020-10-15 15:42:16 +03:00
|
|
|
pub fn u32_in_range(min u32, max u32) u32 {
|
2021-03-03 09:36:56 +03:00
|
|
|
return default_rng.u32_in_range(min, max)
|
2020-06-09 16:06:07 +03:00
|
|
|
}
|
|
|
|
|
2020-12-27 21:06:17 +03:00
|
|
|
// u64_in_range returns a uniformly distributed pseudorandom 64-bit unsigned `u64` in range `[min, max)`.
|
2020-10-15 15:42:16 +03:00
|
|
|
pub fn u64_in_range(min u64, max u64) u64 {
|
2021-03-03 09:36:56 +03:00
|
|
|
return default_rng.u64_in_range(min, max)
|
2020-06-09 16:06:07 +03:00
|
|
|
}
|
|
|
|
|
2020-12-27 21:06:17 +03:00
|
|
|
// int returns a uniformly distributed pseudorandom 32-bit signed (possibly negative) `int`.
|
2020-06-09 16:06:07 +03:00
|
|
|
pub fn int() int {
|
2021-03-03 09:36:56 +03:00
|
|
|
return default_rng.int()
|
2020-06-09 16:06:07 +03:00
|
|
|
}
|
|
|
|
|
2020-12-27 21:06:17 +03:00
|
|
|
// intn returns a uniformly distributed pseudorandom 32-bit signed positive `int` in range `[0, max)`.
|
2020-06-09 16:06:07 +03:00
|
|
|
pub fn intn(max int) int {
|
2021-03-03 09:36:56 +03:00
|
|
|
return default_rng.intn(max)
|
2020-06-09 16:06:07 +03:00
|
|
|
}
|
|
|
|
|
2021-01-26 20:26:05 +03:00
|
|
|
// byte returns a uniformly distributed pseudorandom 8-bit unsigned positive `byte`.
|
|
|
|
pub fn byte() byte {
|
2021-03-03 09:36:56 +03:00
|
|
|
return byte(default_rng.u32() & 0xff)
|
2021-01-26 20:26:05 +03:00
|
|
|
}
|
|
|
|
|
2020-12-27 21:06:17 +03:00
|
|
|
// 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`.
|
2020-10-15 15:42:16 +03:00
|
|
|
pub fn int_in_range(min int, max int) int {
|
2021-03-03 09:36:56 +03:00
|
|
|
return default_rng.int_in_range(min, max)
|
2020-06-09 16:06:07 +03:00
|
|
|
}
|
|
|
|
|
2020-12-27 21:06:17 +03:00
|
|
|
// int31 returns a uniformly distributed pseudorandom 31-bit signed positive `int`.
|
2020-06-09 16:06:07 +03:00
|
|
|
pub fn int31() int {
|
2021-03-03 09:36:56 +03:00
|
|
|
return default_rng.int31()
|
2020-06-09 16:06:07 +03:00
|
|
|
}
|
|
|
|
|
2020-12-27 21:06:17 +03:00
|
|
|
// i64 returns a uniformly distributed pseudorandom 64-bit signed (possibly negative) `i64`.
|
2020-06-09 16:06:07 +03:00
|
|
|
pub fn i64() i64 {
|
2021-03-03 09:36:56 +03:00
|
|
|
return default_rng.i64()
|
2020-06-09 16:06:07 +03:00
|
|
|
}
|
|
|
|
|
2020-12-27 21:06:17 +03:00
|
|
|
// i64n returns a uniformly distributed pseudorandom 64-bit signed positive `i64` in range `[0, max)`.
|
2020-06-09 16:06:07 +03:00
|
|
|
pub fn i64n(max i64) i64 {
|
2021-03-03 09:36:56 +03:00
|
|
|
return default_rng.i64n(max)
|
2020-06-09 16:06:07 +03:00
|
|
|
}
|
|
|
|
|
2020-12-27 21:06:17 +03:00
|
|
|
// i64_in_range returns a uniformly distributed pseudorandom 64-bit signed `i64` in range `[min, max)`.
|
2020-10-15 15:42:16 +03:00
|
|
|
pub fn i64_in_range(min i64, max i64) i64 {
|
2021-03-03 09:36:56 +03:00
|
|
|
return default_rng.i64_in_range(min, max)
|
2020-06-09 16:06:07 +03:00
|
|
|
}
|
|
|
|
|
2020-12-27 21:06:17 +03:00
|
|
|
// int63 returns a uniformly distributed pseudorandom 63-bit signed positive `i64`.
|
2020-06-09 16:06:07 +03:00
|
|
|
pub fn int63() i64 {
|
2021-03-03 09:36:56 +03:00
|
|
|
return default_rng.int63()
|
2020-06-01 22:13:56 +03:00
|
|
|
}
|
|
|
|
|
2020-12-27 21:06:17 +03:00
|
|
|
// f32 returns a uniformly distributed 32-bit floating point in range `[0, 1)`.
|
2020-06-09 16:06:07 +03:00
|
|
|
pub fn f32() f32 {
|
2021-03-03 09:36:56 +03:00
|
|
|
return default_rng.f32()
|
2020-05-27 16:41:37 +03:00
|
|
|
}
|
|
|
|
|
2020-12-27 21:06:17 +03:00
|
|
|
// f64 returns a uniformly distributed 64-bit floating point in range `[0, 1)`.
|
2020-06-09 16:06:07 +03:00
|
|
|
pub fn f64() f64 {
|
2021-03-03 09:36:56 +03:00
|
|
|
return default_rng.f64()
|
2020-05-27 16:41:37 +03:00
|
|
|
}
|
|
|
|
|
2020-12-27 21:06:17 +03:00
|
|
|
// f32n returns a uniformly distributed 32-bit floating point in range `[0, max)`.
|
2020-06-09 16:06:07 +03:00
|
|
|
pub fn f32n(max f32) f32 {
|
2021-03-03 09:36:56 +03:00
|
|
|
return default_rng.f32n(max)
|
2020-05-27 16:41:37 +03:00
|
|
|
}
|
|
|
|
|
2020-12-27 21:06:17 +03:00
|
|
|
// f64n returns a uniformly distributed 64-bit floating point in range `[0, max)`.
|
2020-06-09 16:06:07 +03:00
|
|
|
pub fn f64n(max f64) f64 {
|
2021-03-03 09:36:56 +03:00
|
|
|
return default_rng.f64n(max)
|
2020-05-28 02:16:17 +03:00
|
|
|
}
|
|
|
|
|
2020-12-27 21:06:17 +03:00
|
|
|
// f32_in_range returns a uniformly distributed 32-bit floating point in range `[min, max)`.
|
2020-10-15 15:42:16 +03:00
|
|
|
pub fn f32_in_range(min f32, max f32) f32 {
|
2021-03-03 09:36:56 +03:00
|
|
|
return default_rng.f32_in_range(min, max)
|
2020-05-27 20:13:57 +03:00
|
|
|
}
|
2020-05-28 02:16:17 +03:00
|
|
|
|
2020-12-27 21:06:17 +03:00
|
|
|
// f64_in_range returns a uniformly distributed 64-bit floating point in range `[min, max)`.
|
2020-10-15 15:42:16 +03:00
|
|
|
pub fn f64_in_range(min f64, max f64) f64 {
|
2021-03-03 09:36:56 +03:00
|
|
|
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 (
|
2021-02-19 12:16:02 +03:00
|
|
|
english_letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
|
|
|
hex_chars = 'abcdef0123456789'
|
|
|
|
ascii_chars = '!"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ\\^_`abcdefghijklmnopqrstuvwxyz{|}~'
|
2020-07-15 22:36:06 +03:00
|
|
|
)
|
|
|
|
|
2021-02-19 12:16:02 +03:00
|
|
|
// string_from_set returns a string of length `len` containing random characters sampled from the given `charset`
|
|
|
|
pub fn string_from_set(charset string, len int) string {
|
|
|
|
if len == 0 {
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
mut buf := unsafe { malloc(len) }
|
2020-07-25 20:20:37 +03:00
|
|
|
for i in 0 .. len {
|
2020-07-22 20:28:53 +03:00
|
|
|
unsafe {
|
2021-02-19 12:16:02 +03:00
|
|
|
buf[i] = charset[intn(charset.len)]
|
2020-07-22 20:28:53 +03:00
|
|
|
}
|
2020-07-15 22:36:06 +03:00
|
|
|
}
|
2021-01-26 16:55:09 +03:00
|
|
|
return unsafe { buf.vstring_with_len(len) }
|
2020-07-15 22:36:06 +03:00
|
|
|
}
|
2020-07-24 13:58:30 +03:00
|
|
|
|
2021-02-19 12:16:02 +03:00
|
|
|
// string returns a string of length `len` containing random characters in range `[a-zA-Z]`.
|
|
|
|
pub fn string(len int) string {
|
|
|
|
return string_from_set(rand.english_letters, len)
|
|
|
|
}
|
2021-02-10 13:01:25 +03:00
|
|
|
|
|
|
|
// hex returns a hexadecimal number of length `len` containing random characters in range `[a-f0-9]`.
|
|
|
|
pub fn hex(len int) string {
|
2021-02-19 12:16:02 +03:00
|
|
|
return string_from_set(rand.hex_chars, len)
|
2021-02-10 13:01:25 +03:00
|
|
|
}
|
|
|
|
|
2021-02-11 09:32:40 +03:00
|
|
|
// ascii returns a random string of the printable ASCII characters with length `len`.
|
|
|
|
pub fn ascii(len int) string {
|
2021-02-19 12:16:02 +03:00
|
|
|
return string_from_set(rand.ascii_chars, len)
|
2021-02-11 09:32:40 +03:00
|
|
|
}
|
|
|
|
|
2020-12-27 21:06:17 +03:00
|
|
|
// uuid_v4 generates a random (v4) UUID
|
2020-07-24 13:58:30 +03:00
|
|
|
// See https://en.wikipedia.org/wiki/Universally_unique_identifier#Version_4_(random)
|
|
|
|
pub fn uuid_v4() string {
|
2020-07-25 21:39:54 +03:00
|
|
|
buflen := 36
|
2021-02-19 12:16:02 +03:00
|
|
|
mut buf := unsafe { malloc(37) }
|
2020-07-25 13:09:05 +03:00
|
|
|
mut i_buf := 0
|
2020-07-25 21:39:54 +03:00
|
|
|
mut x := u64(0)
|
|
|
|
mut d := byte(0)
|
|
|
|
for i_buf < buflen {
|
2020-07-25 13:09:05 +03:00
|
|
|
mut c := 0
|
2021-03-03 09:36:56 +03:00
|
|
|
x = default_rng.u64()
|
2020-07-25 21:39:54 +03:00
|
|
|
// do most of the bit manipulation at once:
|
|
|
|
x &= 0x0F0F0F0F0F0F0F0F
|
|
|
|
x += 0x3030303030303030
|
|
|
|
// write the ASCII codes to the buffer:
|
|
|
|
for c < 8 && i_buf < buflen {
|
|
|
|
d = byte(x)
|
2020-07-25 13:09:05 +03:00
|
|
|
unsafe {
|
2020-07-25 21:39:54 +03:00
|
|
|
buf[i_buf] = if d > 0x39 { d + 0x27 } else { d }
|
2020-07-25 13:09:05 +03:00
|
|
|
}
|
|
|
|
i_buf++
|
|
|
|
c++
|
2020-07-25 21:39:54 +03:00
|
|
|
x = x >> 8
|
2020-07-24 13:58:30 +03:00
|
|
|
}
|
|
|
|
}
|
2020-07-25 21:39:54 +03:00
|
|
|
// there are still some random bits in x:
|
|
|
|
x = x >> 8
|
|
|
|
d = byte(x)
|
2020-07-24 13:58:30 +03:00
|
|
|
unsafe {
|
2020-07-25 21:39:54 +03:00
|
|
|
buf[19] = if d > 0x39 { d + 0x27 } else { d }
|
2020-07-24 13:58:30 +03:00
|
|
|
buf[8] = `-`
|
|
|
|
buf[13] = `-`
|
|
|
|
buf[18] = `-`
|
|
|
|
buf[23] = `-`
|
2020-07-25 21:39:54 +03:00
|
|
|
buf[14] = `4`
|
|
|
|
buf[buflen] = 0
|
2021-02-14 21:31:42 +03:00
|
|
|
return buf.vstring_with_len(buflen)
|
2020-07-24 13:58:30 +03:00
|
|
|
}
|
|
|
|
}
|
2020-07-26 13:10:56 +03:00
|
|
|
|
2020-10-15 15:42:16 +03:00
|
|
|
const (
|
|
|
|
ulid_encoding = '0123456789ABCDEFGHJKMNPQRSTVWXYZ'
|
2020-07-26 13:10:56 +03:00
|
|
|
)
|
|
|
|
|
2020-12-27 21:06:17 +03:00
|
|
|
// ulid generates an Unique Lexicographically sortable IDentifier.
|
2020-07-26 13:10:56 +03:00
|
|
|
// See https://github.com/ulid/spec .
|
|
|
|
// NB: ULIDs can leak timing information, if you make them public, because
|
2020-10-15 15:42:16 +03:00
|
|
|
// you can infer the rate at which some resource is being created, like
|
2020-07-26 13:10:56 +03:00
|
|
|
// users or business transactions.
|
|
|
|
// (https://news.ycombinator.com/item?id=14526173)
|
|
|
|
pub fn ulid() string {
|
2020-07-26 14:09:49 +03:00
|
|
|
return ulid_at_millisecond(time.utc().unix_time_milli())
|
|
|
|
}
|
|
|
|
|
2020-12-27 21:06:17 +03:00
|
|
|
// ulid_at_millisecond does the same as `ulid` but takes a custom Unix millisecond timestamp via `unix_time_milli`.
|
2020-07-26 14:09:49 +03:00
|
|
|
pub fn ulid_at_millisecond(unix_time_milli u64) string {
|
2020-07-26 13:10:56 +03:00
|
|
|
buflen := 26
|
2021-02-19 12:16:02 +03:00
|
|
|
mut buf := unsafe { malloc(27) }
|
2020-07-26 14:09:49 +03:00
|
|
|
mut t := unix_time_milli
|
2020-07-26 13:10:56 +03:00
|
|
|
mut i := 9
|
|
|
|
for i >= 0 {
|
2020-10-15 15:42:16 +03:00
|
|
|
unsafe {
|
2021-01-26 16:55:09 +03:00
|
|
|
buf[i] = rand.ulid_encoding[t & 0x1F]
|
2020-07-26 13:10:56 +03:00
|
|
|
}
|
|
|
|
t = t >> 5
|
|
|
|
i--
|
|
|
|
}
|
|
|
|
// first rand set
|
2021-03-03 09:36:56 +03:00
|
|
|
mut x := default_rng.u64()
|
2020-07-26 13:10:56 +03:00
|
|
|
i = 10
|
|
|
|
for i < 19 {
|
2020-10-15 15:42:16 +03:00
|
|
|
unsafe {
|
2021-01-26 16:55:09 +03:00
|
|
|
buf[i] = rand.ulid_encoding[x & 0x1F]
|
2020-07-26 13:10:56 +03:00
|
|
|
}
|
|
|
|
x = x >> 5
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
// second rand set
|
2021-03-03 09:36:56 +03:00
|
|
|
x = default_rng.u64()
|
2020-07-26 13:10:56 +03:00
|
|
|
for i < 26 {
|
2020-10-15 15:42:16 +03:00
|
|
|
unsafe {
|
2021-01-26 16:55:09 +03:00
|
|
|
buf[i] = rand.ulid_encoding[x & 0x1F]
|
2020-07-26 13:10:56 +03:00
|
|
|
}
|
|
|
|
x = x >> 5
|
|
|
|
i++
|
|
|
|
}
|
2020-10-15 15:42:16 +03:00
|
|
|
unsafe {
|
2020-07-26 13:10:56 +03:00
|
|
|
buf[26] = 0
|
2021-02-14 21:31:42 +03:00
|
|
|
return buf.vstring_with_len(buflen)
|
2020-07-26 13:10:56 +03:00
|
|
|
}
|
|
|
|
}
|