| 1 | module rand |
| 2 | |
| 3 | // init initializes the default RNG. |
| 4 | fn init() { |
| 5 | default_rng = new_default() |
| 6 | } |
| 7 | |
| 8 | fn internal_fill_buffer_from_set(mut _ PRNG, _ string, mut _ []u8) { |
| 9 | panic('todo') |
| 10 | } |
| 11 | |
| 12 | // fn internal_string_from_set(mut rng PRNG, charset string, len int) string { |
| 13 | fn internal_string_from_set(mut _ PRNG, charset string, len int) string { |
| 14 | result := '' |
| 15 | # |
| 16 | #const characters = charset.str; |
| 17 | #const charactersLength = characters.length; |
| 18 | #for (let i = 0;i < len.val;i++) |
| 19 | #result.str += characters.charAt(Math.random() * charactersLength); |
| 20 | |
| 21 | _ = charset |
| 22 | _ = len |
| 23 | |
| 24 | return result |
| 25 | } |
| 26 | |
| 27 | const ulid_encoding = '0123456789ABCDEFGHJKMNPQRSTVWXYZ' |
| 28 | |
| 29 | fn internal_ulid_at_millisecond(mut rng PRNG, unix_time_milli u64) string { |
| 30 | mut buf := []u8{cap: 27} |
| 31 | mut t := unix_time_milli |
| 32 | mut i := 9 |
| 33 | for i >= 0 { |
| 34 | buf[i] = ulid_encoding[int(t & 0x1f)] |
| 35 | t = t >> 5 |
| 36 | i-- |
| 37 | } |
| 38 | |
| 39 | mut x := rng.u64() |
| 40 | i = 10 |
| 41 | for i < 19 { |
| 42 | buf[i] = ulid_encoding[int(x & 0x1f)] |
| 43 | |
| 44 | x = x >> 5 |
| 45 | i++ |
| 46 | } |
| 47 | |
| 48 | x = rng.u64() |
| 49 | for i < 26 { |
| 50 | buf[i] = ulid_encoding[int(x & 0x1f)] |
| 51 | x = x >> 5 |
| 52 | i++ |
| 53 | } |
| 54 | |
| 55 | res := '' |
| 56 | println(buf) |
| 57 | #res.str = buf.arr.arr.map(String.fromCharCode).join('') |
| 58 | |
| 59 | return res |
| 60 | } |
| 61 | |
| 62 | fn read_internal(mut rng PRNG, mut buf []u8) { |
| 63 | for i in 0 .. buf.len { |
| 64 | buf[i] = rng.u8() |
| 65 | } |
| 66 | } |
| 67 | |