decrease gap between PRNG interface and rand module functions

pull/13719/head
Subhomoy Haldar 2022-03-12 01:09:12 +05:30
parent ba4cb35251
commit 807cd408f7
3 changed files with 85 additions and 48 deletions

View File

@ -7,6 +7,10 @@ const clock_seq_hi_and_reserved_valid_values = [`8`, `9`, `a`, `b`]
// uuid_v4 generates a random (v4) UUID // uuid_v4 generates a random (v4) UUID
// See https://en.wikipedia.org/wiki/Universally_unique_identifier#Version_4_(random) // See https://en.wikipedia.org/wiki/Universally_unique_identifier#Version_4_(random)
pub fn uuid_v4() string { pub fn uuid_v4() string {
return internal_uuid_v4(mut default_rng)
}
fn internal_uuid_v4(mut rng PRNG) string {
buflen := 36 buflen := 36
mut buf := unsafe { malloc_noscan(37) } mut buf := unsafe { malloc_noscan(37) }
mut i_buf := 0 mut i_buf := 0
@ -14,7 +18,7 @@ pub fn uuid_v4() string {
mut d := byte(0) mut d := byte(0)
for i_buf < buflen { for i_buf < buflen {
mut c := 0 mut c := 0
x = default_rng.u64() x = rng.u64()
// do most of the bit manipulation at once: // do most of the bit manipulation at once:
x &= 0x0F0F0F0F0F0F0F0F x &= 0x0F0F0F0F0F0F0F0F
x += 0x3030303030303030 x += 0x3030303030303030
@ -54,18 +58,7 @@ pub fn uuid_v4() string {
const ulid_encoding = '0123456789ABCDEFGHJKMNPQRSTVWXYZ' const ulid_encoding = '0123456789ABCDEFGHJKMNPQRSTVWXYZ'
// ulid generates an Unique Lexicographically sortable IDentifier. fn internal_ulid_at_millisecond(mut rng PRNG, unix_time_milli u64) string {
// See https://github.com/ulid/spec .
// Note: ULIDs can leak timing information, if you make them public, because
// you can infer the rate at which some resource is being created, like
// users or business transactions.
// (https://news.ycombinator.com/item?id=14526173)
pub fn ulid() string {
return ulid_at_millisecond(u64(time.utc().unix_time_milli()))
}
// ulid_at_millisecond does the same as `ulid` but takes a custom Unix millisecond timestamp via `unix_time_milli`.
pub fn ulid_at_millisecond(unix_time_milli u64) string {
buflen := 26 buflen := 26
mut buf := unsafe { malloc_noscan(27) } mut buf := unsafe { malloc_noscan(27) }
mut t := unix_time_milli mut t := unix_time_milli
@ -78,7 +71,7 @@ pub fn ulid_at_millisecond(unix_time_milli u64) string {
i-- i--
} }
// first rand set // first rand set
mut x := default_rng.u64() mut x := rng.u64()
i = 10 i = 10
for i < 19 { for i < 19 {
unsafe { unsafe {
@ -88,7 +81,7 @@ pub fn ulid_at_millisecond(unix_time_milli u64) string {
i++ i++
} }
// second rand set // second rand set
x = default_rng.u64() x = rng.u64()
for i < 26 { for i < 26 {
unsafe { unsafe {
buf[i] = rand.ulid_encoding[x & 0x1F] buf[i] = rand.ulid_encoding[x & 0x1F]
@ -102,8 +95,7 @@ pub fn ulid_at_millisecond(unix_time_milli u64) string {
} }
} }
// string_from_set returns a string of length `len` containing random characters sampled from the given `charset` fn internal_string_from_set(mut rng PRNG, charset string, len int) string {
pub fn string_from_set(charset string, len int) string {
if len == 0 { if len == 0 {
return '' return ''
} }
@ -119,21 +111,6 @@ pub fn string_from_set(charset string, len int) string {
return unsafe { buf.vstring_with_len(len) } return unsafe { buf.vstring_with_len(len) }
} }
// 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(english_letters, len)
}
// hex returns a hexadecimal number of length `len` containing random characters in range `[a-f0-9]`.
pub fn hex(len int) string {
return string_from_set(hex_chars, len)
}
// ascii returns a random string of the printable ASCII characters with length `len`.
pub fn ascii(len int) string {
return string_from_set(ascii_chars, len)
}
fn deinit() { fn deinit() {
unsafe { unsafe {
default_rng.free() // free the implementation default_rng.free() // free the implementation

View File

@ -7,10 +7,10 @@ fn init() {
default_rng = new_default() default_rng = new_default()
} }
pub fn string(len int) string { fn internal_string_from_set(mut rng PRNG, charset string, len int) string {
result := '' result := ''
# #
#const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; #const characters = charset.str;
#const charactersLength = characters.length; #const charactersLength = characters.length;
#for (let i = 0;i < len.val;i++) #for (let i = 0;i < len.val;i++)
#result.str += characters.charAt(Math.random() * charactersLength); #result.str += characters.charAt(Math.random() * charactersLength);
@ -22,18 +22,7 @@ const (
ulid_encoding = '0123456789ABCDEFGHJKMNPQRSTVWXYZ' ulid_encoding = '0123456789ABCDEFGHJKMNPQRSTVWXYZ'
) )
// ulid generates an Unique Lexicographically sortable IDentifier. fn internal_ulid_at_millisecond(mut rng PRNG, unix_time_milli u64) string {
// See https://github.com/ulid/spec .
// Note: ULIDs can leak timing information, if you make them public, because
// you can infer the rate at which some resource is being created, like
// users or business transactions.
// (https://news.ycombinator.com/item?id=14526173)
pub fn ulid() string {
return ulid_at_millisecond(u64(time.utc().unix_time_milli()))
}
// ulid_at_millisecond does the same as `ulid` but takes a custom Unix millisecond timestamp via `unix_time_milli`.
pub fn ulid_at_millisecond(unix_time_milli u64) string {
mut buf := []byte{cap: 27} mut buf := []byte{cap: 27}
mut t := unix_time_milli mut t := unix_time_milli
mut i := 9 mut i := 9
@ -43,7 +32,7 @@ pub fn ulid_at_millisecond(unix_time_milli u64) string {
i-- i--
} }
mut x := default_rng.u64() mut x := rng.u64()
i = 10 i = 10
for i < 19 { for i < 19 {
buf[i] = rand.ulid_encoding[int(x & 0x1f)] buf[i] = rand.ulid_encoding[int(x & 0x1f)]
@ -52,7 +41,7 @@ pub fn ulid_at_millisecond(unix_time_milli u64) string {
i++ i++
} }
x = default_rng.u64() x = rng.u64()
for i < 26 { for i < 26 {
buf[i] = rand.ulid_encoding[int(x & 0x1f)] buf[i] = rand.ulid_encoding[int(x & 0x1f)]
x = x >> 5 x = x >> 5

View File

@ -8,6 +8,7 @@ import math.bits
import rand.config import rand.config
import rand.constants import rand.constants
import rand.wyrand import rand.wyrand
import time
// PRNG is a common interface for all PRNGs that can be used seamlessly with the rand // 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 // modules's API. It defines all the methods that a PRNG (in the vlib or custom made) must
@ -238,6 +239,41 @@ pub fn (mut rng PRNG) f64_in_range(min f64, max f64) ?f64 {
return min + rng.f64n(max - min) ? return min + rng.f64n(max - min) ?
} }
// ulid generates an Unique Lexicographically sortable IDentifier.
// See https://github.com/ulid/spec .
// Note: ULIDs can leak timing information, if you make them public, because
// you can infer the rate at which some resource is being created, like
// users or business transactions.
// (https://news.ycombinator.com/item?id=14526173)
pub fn (mut rng PRNG) ulid() string {
return internal_ulid_at_millisecond(mut rng, u64(time.utc().unix_time_milli()))
}
// ulid_at_millisecond does the same as `ulid` but takes a custom Unix millisecond timestamp via `unix_time_milli`.
pub fn (mut rng PRNG) ulid_at_millisecond(unix_time_milli u64) string {
return internal_ulid_at_millisecond(mut rng, unix_time_milli)
}
// string_from_set returns a string of length `len` containing random characters sampled from the given `charset`
pub fn (mut rng PRNG) string_from_set(charset string, len int) string {
return internal_string_from_set(mut rng, charset, len)
}
// string returns a string of length `len` containing random characters in range `[a-zA-Z]`.
pub fn (mut rng PRNG) string(len int) string {
return internal_string_from_set(mut rng, rand.english_letters, len)
}
// hex returns a hexadecimal number of length `len` containing random characters in range `[a-f0-9]`.
pub fn (mut rng PRNG) hex(len int) string {
return internal_string_from_set(mut rng, rand.hex_chars, len)
}
// ascii returns a random string of the printable ASCII characters with length `len`.
pub fn (mut rng PRNG) ascii(len int) string {
return internal_string_from_set(mut rng, rand.ascii_chars, len)
}
__global default_rng &PRNG __global default_rng &PRNG
// 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. // 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.
@ -396,3 +432,38 @@ const (
hex_chars = 'abcdef0123456789' hex_chars = 'abcdef0123456789'
ascii_chars = '!"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ\\^_`abcdefghijklmnopqrstuvwxyz{|}~' ascii_chars = '!"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ\\^_`abcdefghijklmnopqrstuvwxyz{|}~'
) )
// ulid generates an Unique Lexicographically sortable IDentifier.
// See https://github.com/ulid/spec .
// Note: ULIDs can leak timing information, if you make them public, because
// you can infer the rate at which some resource is being created, like
// users or business transactions.
// (https://news.ycombinator.com/item?id=14526173)
pub fn ulid() string {
return internal_ulid_at_millisecond(mut default_rng, u64(time.utc().unix_time_milli()))
}
// ulid_at_millisecond does the same as `ulid` but takes a custom Unix millisecond timestamp via `unix_time_milli`.
pub fn ulid_at_millisecond(unix_time_milli u64) string {
return internal_ulid_at_millisecond(mut default_rng, unix_time_milli)
}
// 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 {
return internal_string_from_set(mut default_rng, charset, len)
}
// 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)
}
// hex returns a hexadecimal number of length `len` containing random characters in range `[a-f0-9]`.
pub fn hex(len int) string {
return string_from_set(rand.hex_chars, len)
}
// ascii returns a random string of the printable ASCII characters with length `len`.
pub fn ascii(len int) string {
return string_from_set(rand.ascii_chars, len)
}