Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose current internal random number generator seed to scripts #5615

Open
BevanFindlay opened this issue Oct 19, 2022 · 2 comments
Open

Expose current internal random number generator seed to scripts #5615

BevanFindlay opened this issue Oct 19, 2022 · 2 comments

Comments

@BevanFindlay
Copy link

BevanFindlay commented Oct 19, 2022

Describe the project you are working on

A game that uses various forms of randomness for terrain generation and placing and naming cities and objects (randi, randf, randfn, OpenSimplexNoise, Array.shuffle(), etc).

Describe the problem or limitation you are having in your project

When bug testing, I want the ability to recreate the exact scenario that caused a particular issue. This means being able to initialise the random number generator (RNG) with the same starting conditions. I can set up my own object (RandomNumberGenerator.new()) and use that for most things, but the internal RNG is still used for things like Array.shuffle().

It is possible to do randomize() then set a variable using randi() then use seed(variable) to get a non-deterministic but known seed, however it's not a straightforward nor clean way of doing it.

Describe the feature / enhancement and how it helps to overcome the problem or limitation

Simply have something that exposes the current internal RNG's seed. e.g. the same as rng_variable.get_seed() for an RNG created using rng_variable = RandomNumberGenerator.new()

Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams

As above: simply a command that returns the current internal RNG's seed. e.g. get_seed() as a standalone command, instead of rng_variable.get_seed()

If this enhancement will not be used often, can it be worked around with a few lines of script?

randomize()
var new_seed = randi()
seed(new_seed)

print(new_seed)

Is there a reason why this should be core and not an add-on in the asset library?

Having looked at questions on the 'net, I am not the first to ask how to do this. It should be a really simple change to make this available, and the workaround is not intuitive and was quite hard to find. The documentation for RandomNumberGenerator suggests that we should be able to use get_seed() on any instance of the RNG, but we cannot use that on the internal one. This would make the internal RNG more consistent with the documented features of RNGs in general.

@dalexeev
Copy link
Member

There is RandomNumberGenerator.state.

but the internal RNG is still used for things like Array.shuffle().

This is a good point, at least we can clarify the documentation of this method. But I'm still not sure about the need to control the global RNG seed/state (randomize, seed, and potential method for getting/changing state). The global RNG is poorly controlled (as it can be changed from anywhere) and in my opinion should not be used for reproducible sequences.

Perhaps we should fix a few problematic methods: Array.shuffle(rng: RandomNumberGenerator = null) or Array.shuffle_rng(rng: RandomNumberGenerator).

@Misza13
Copy link

Misza13 commented Mar 2, 2023

Another syntax alternative: rng.shuffle(array). RandomNumberGenerator already has methods for generating numbers, so I believe that's where the shuffle method belongs (not on an array).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants