rand: generate hexadecimal numbers (#8664)

pull/8672/head
cbracketdash 2021-02-10 02:01:25 -08:00 committed by GitHub
parent f2e74bce7a
commit eb7009b60a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 16 additions and 0 deletions

View File

@ -152,6 +152,22 @@ pub fn string(len int) string {
return unsafe { buf.vstring_with_len(len) } return unsafe { buf.vstring_with_len(len) }
} }
const (
hex_chars = 'abcdef0123456789'
)
// hex returns a hexadecimal number of length `len` containing random characters in range `[a-f0-9]`.
pub fn hex(len int) string {
mut buf := malloc(len)
for i in 0 .. len {
unsafe {
buf[i] = rand.hex_chars[intn(rand.hex_chars.len)]
}
}
return unsafe { buf.vstring_with_len(len) }
}
// 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 {