Any way to get the current seed?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Nobuyuki

Hi! First time using Godot. I am playing around with a demo and have figured out how to set a seed manually. However, I’m curious to know what seed I’m getting when I call randomize(). Is there any way to get the current seed? It could be helpful for reproducing a set of random numbers.

Alternatively, is there a gdscript equivalent to the number randomize() generates based on time that can be used with seed()?

:bust_in_silhouette: Reply From: avencherus

I don’t believe so, I do wish that randomize() returned the seed it generated.

The work around is that you would first randomize(), generate a random number to use as your seed, and then call seed() with it. That way you have the seed stored if you need to save it and restore it at some point later.

I’m not to sure what rand_seed() does, it seems rather vaguely documented, and spits out a [0, some number].

You may write a function like this:

func new_seed():

	randomize()
	var random_seed = randi()
	seed(random_seed)
	return random_seed
:bust_in_silhouette: Reply From: Fattywithhacks

There is a way by using the get_seed() method. but it only works when creating a new RandomNumberGenerator by using RandomNumberGenerator.new()

var RNG = RandomNumberGenerator.new()

func _ready():
	RNG.randomize() 
	var current_seed = RNG.get_seed() 

if you for e.g. would like to generate a random position you must use the RNG to keep the seed

var random_number = RNG.randi()

sorry for my bad english, I hope you could understand this :smiley:
for more about the RandomNumberGenerator read the docs :slight_smile:

This is perfect! Thank you. Strange that they wouldn’t have just a global RandomNumberGenerator that you can directly use instead of pass-throughs of randi(), rand_range(), etc. I just made an autoload-singleton and threw a random number generator object on it that is setup in the singleton’s _ready() call and use that around my project in order to have a deterministic randomization system. Useful for saved games and multiplayer.

Underflow Studios | 2020-11-04 19:06