How to get current context.random_generator seed?

Title says the thing :slight_smile:

I found out that i can set the seed with rand.reset(myseed). But how can get the current seed (e.g. for debug purposes)?

Because we now have configurable random generators, it depends on what type of generator you’re using in any particular context. For the default context.random_generator, the state is stored inside a thread local proc-internal variable, but you can easily make your own with state accessible outside of that.

package main

import "base:runtime"
import "core:fmt"
import "core:math/rand"

main :: proc() {
	visible_state: runtime.Default_Random_State

	fmt.println(visible_state)

	context.random_generator = runtime.default_random_generator(&visible_state)

	fmt.println(rand.uint32())
	fmt.println(visible_state)
}

However, this will return the entire state of the generator, not the initial seed. If you need to be able to inspect the actual seed of your random generator, it might be ideal to create your own generator implementation with that as part of its state.

Alternatively and more simply, set the seed of your generators with a known number and store that for later retrieval. This is what the Odin test runner does to ensure reproducibility of random-influenced tests.

1 Like

Thanks! I think the last option is the way to go