0 votes

I'm using a RandomNumberGenerator to help produce a procedurally generated map, and I want its values to be consistent between generations by seeding the RandomNumberGenerator.

From the output it seems to me like the RandomNumberGenerator functions change the seed after each call. For example, the the seed changes after running this line:

        var weight = rng.randf_range(1.0, NOISE_VARIANCE)

Is there a way to prevent the seed from changing so I can save the seed for each map generation?

in Engine by (135 points)

1 Answer

0 votes

The RandomNumberGenerator object has a seed property that will allow you to provide a seed value. Using that, you should be able to generate a "random", yet "reproducible", set of values.

https://docs.godotengine.org/en/stable/classes/class_randomnumbergenerator.html

So, for example:

func _ready():
    var rng = RandomNumberGenerator.new()
    rng.seed = 11
    for i in range(4):
        print(rng.randi())

    print("\n")

    rng = RandomNumberGenerator.new()
    rng.seed = 11
    for i in range(4):
        print(rng.randi())

The above creates 2 separate RandomNumberGenerator instances, seeds them both with the same value (11) and then prints their first 4 returned values - which are the same.

So, as long as you feed the object a common seed, you should get the same stream of "random" values. A different seed will change the stream of values...

by (12,471 points)
edited by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.