2021-10-01 21:23:49 +03:00
|
|
|
module rand
|
|
|
|
|
|
|
|
// init initializes the default RNG.
|
|
|
|
fn init() {
|
|
|
|
default_rng = new_default()
|
|
|
|
}
|
|
|
|
|
2022-03-12 10:41:12 +03:00
|
|
|
fn internal_string_from_set(mut rng PRNG, charset string, len int) string {
|
2021-10-01 21:23:49 +03:00
|
|
|
result := ''
|
|
|
|
#
|
2022-03-12 10:41:12 +03:00
|
|
|
#const characters = charset.str;
|
2021-10-01 21:23:49 +03:00
|
|
|
#const charactersLength = characters.length;
|
|
|
|
#for (let i = 0;i < len.val;i++)
|
|
|
|
#result.str += characters.charAt(Math.random() * charactersLength);
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
2021-10-13 09:40:14 +03:00
|
|
|
|
|
|
|
const (
|
|
|
|
ulid_encoding = '0123456789ABCDEFGHJKMNPQRSTVWXYZ'
|
|
|
|
)
|
|
|
|
|
2022-03-12 10:41:12 +03:00
|
|
|
fn internal_ulid_at_millisecond(mut rng PRNG, unix_time_milli u64) string {
|
2022-04-15 15:35:35 +03:00
|
|
|
mut buf := []u8{cap: 27}
|
2021-10-13 09:40:14 +03:00
|
|
|
mut t := unix_time_milli
|
|
|
|
mut i := 9
|
|
|
|
for i >= 0 {
|
|
|
|
buf[i] = rand.ulid_encoding[int(t & 0x1f)]
|
|
|
|
t = t >> 5
|
|
|
|
i--
|
|
|
|
}
|
|
|
|
|
2022-03-12 10:41:12 +03:00
|
|
|
mut x := rng.u64()
|
2021-10-13 09:40:14 +03:00
|
|
|
i = 10
|
|
|
|
for i < 19 {
|
|
|
|
buf[i] = rand.ulid_encoding[int(x & 0x1f)]
|
|
|
|
|
|
|
|
x = x >> 5
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
2022-03-12 10:41:12 +03:00
|
|
|
x = rng.u64()
|
2021-10-13 09:40:14 +03:00
|
|
|
for i < 26 {
|
|
|
|
buf[i] = rand.ulid_encoding[int(x & 0x1f)]
|
|
|
|
x = x >> 5
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
|
|
|
res := ''
|
|
|
|
println(buf)
|
|
|
|
#res.str = buf.arr.arr.map(String.fromCharCode).join('')
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
2022-02-06 11:49:43 +03:00
|
|
|
|
2022-04-15 15:35:35 +03:00
|
|
|
fn read_internal(mut rng PRNG, mut buf []u8) {
|
2022-02-06 11:49:43 +03:00
|
|
|
for i in 0 .. buf.len {
|
2022-04-15 14:58:56 +03:00
|
|
|
buf[i] = rng.u8()
|
2022-02-06 11:49:43 +03:00
|
|
|
}
|
|
|
|
}
|