mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-07 12:33:42 +00:00
runtime: add runtime.rand function
This function is needed for Go 1.22, and is used from various packages like math/rand. When there is no random number generator available, it falls back to a static sequence of numbers. I think this is fine, because as far as I can see it is only used for non-cryptographic needs.
This commit is contained in:
@@ -120,3 +120,16 @@ func libc_getpagesize() int
|
||||
func syscall_Getpagesize() int {
|
||||
return libc_getpagesize()
|
||||
}
|
||||
|
||||
func hardwareRand() (n uint64, ok bool) {
|
||||
read := libc_getrandom(unsafe.Pointer(&n), 8, 0)
|
||||
if read != 8 {
|
||||
return 0, false
|
||||
}
|
||||
return n, true
|
||||
}
|
||||
|
||||
// ssize_t getrandom(void buf[.buflen], size_t buflen, unsigned int flags);
|
||||
//
|
||||
//export getrandom
|
||||
func libc_getrandom(buf unsafe.Pointer, buflen uintptr, flags uint32) uint32
|
||||
|
||||
Reference in New Issue
Block a user