2021-10-01 20:23:49 +02:00
|
|
|
module rand
|
|
|
|
|
|
|
|
// init initializes the default RNG.
|
|
|
|
fn init() {
|
|
|
|
default_rng = new_default()
|
|
|
|
}
|
|
|
|
|
2022-03-12 08:41:12 +01:00
|
|
|
fn internal_string_from_set(mut rng PRNG, charset string, len int) string {
|
2021-10-01 20:23:49 +02:00
|
|
|
result := ''
|
|
|
|
#
|
2022-03-12 08:41:12 +01:00
|
|
|
#const characters = charset.str;
|
2021-10-01 20:23:49 +02: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 08:40:14 +02:00
|
|
|
|
|
|
|
const (
|
|
|
|
ulid_encoding = '0123456789ABCDEFGHJKMNPQRSTVWXYZ'
|
|
|
|
)
|
|
|
|
|
2022-03-12 08:41:12 +01:00
|
|
|
fn internal_ulid_at_millisecond(mut rng PRNG, unix_time_milli u64) string {
|
2022-04-15 14:35:35 +02:00
|
|
|
mut buf := []u8{cap: 27}
|
2021-10-13 08:40:14 +02: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 08:41:12 +01:00
|
|
|
mut x := rng.u64()
|
2021-10-13 08:40:14 +02:00
|
|
|
i = 10
|
|
|
|
for i < 19 {
|
|
|
|
buf[i] = rand.ulid_encoding[int(x & 0x1f)]
|
|
|
|
|
|
|
|
x = x >> 5
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
2022-03-12 08:41:12 +01:00
|
|
|
x = rng.u64()
|
2021-10-13 08:40:14 +02: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 09:49:43 +01:00
|
|
|
|
2022-04-15 14:35:35 +02:00
|
|
|
fn read_internal(mut rng PRNG, mut buf []u8) {
|
2022-02-06 09:49:43 +01:00
|
|
|
for i in 0 .. buf.len {
|
2022-04-15 13:58:56 +02:00
|
|
|
buf[i] = rng.u8()
|
2022-02-06 09:49:43 +01:00
|
|
|
}
|
|
|
|
}
|