aboutsummaryrefslogtreecommitdiff
path: root/core/math/rand/rand.odin
diff options
context:
space:
mode:
authorYawning Angel <yawning@schwanenlied.me>2024-04-21 21:16:50 +0900
committerYawning Angel <yawning@schwanenlied.me>2024-04-23 11:47:43 +0900
commite2fa9be7e2c02ad950e4f3205f5e67c9ebd3a70c (patch)
treefd5df47d5968012bb4aaaeba57f3cce5f79085fb /core/math/rand/rand.odin
parenta6eb64df6cd136639d1234e5a157ad280a1a32a8 (diff)
core/math/rand: Use `core:crypto` for the system RNG
This removes some code duplication and expands support for the system RNG to all targets that `core:crypto` supports.
Diffstat (limited to 'core/math/rand/rand.odin')
-rw-r--r--core/math/rand/rand.odin39
1 files changed, 21 insertions, 18 deletions
diff --git a/core/math/rand/rand.odin b/core/math/rand/rand.odin
index d6a20bd1e..664d6abc9 100644
--- a/core/math/rand/rand.odin
+++ b/core/math/rand/rand.odin
@@ -5,6 +5,7 @@ Package core:math/rand implements various random number generators
package rand
import "base:intrinsics"
+import "core:crypto"
import "core:math"
import "core:mem"
@@ -104,27 +105,30 @@ init :: proc(r: ^Rand, seed: u64) {
}
/*
-Initialises a random number generator to use the system random number generator.
-The system random number generator is platform specific.
-On `linux` refer to the `getrandom` syscall.
-On `darwin` refer to `getentropy`.
-On `windows` refer to `BCryptGenRandom`.
-
-All other platforms are not supported
+Initialises a random number generator to use the system random number generator.
+The system random number generator is platform specific, and not supported
+on all targets.
Inputs:
- r: The random number generator to use the system random number generator
-WARNING: Panics if the system is not either `windows`, `darwin` or `linux`
+WARNING: Panics if the system random number generator is not supported.
+Support can be determined via the `core:crypto.HAS_RAND_BYTES` constant.
Example:
+ import "core:crypto"
import "core:math/rand"
import "core:fmt"
init_as_system_example :: proc() {
my_rand: rand.Rand
- rand.init_as_system(&my_rand)
- fmt.println(rand.uint64(&my_rand))
+ switch crypto.HAS_RAND_BYTES {
+ case true:
+ rand.init_as_system(&my_rand)
+ fmt.println(rand.uint64(&my_rand))
+ case false:
+ fmt.println("system random not supported!")
+ }
}
Possible Output:
@@ -133,7 +137,7 @@ Possible Output:
*/
init_as_system :: proc(r: ^Rand) {
- if !#defined(_system_random) {
+ if !crypto.HAS_RAND_BYTES {
panic(#procedure + " is not supported on this platform yet")
}
r.state = 0
@@ -144,15 +148,14 @@ init_as_system :: proc(r: ^Rand) {
@(private)
_random_u64 :: proc(r: ^Rand) -> u64 {
r := r
- if r == nil {
+ switch {
+ case r == nil:
r = &global_rand
+ case r.is_system:
+ value: u64
+ crypto.rand_bytes((cast([^]u8)&value)[:size_of(u64)])
+ return value
}
- when #defined(_system_random) {
- if r.is_system {
- return _system_random()
- }
- }
-
old_state := r.state
r.state = old_state * 6364136223846793005 + (r.inc|1)