My random text... is always the same ??!

Hello, i make a chatbot with Godot and i want to create random text from the program to simulate a... forum, or a chat.
Like the others connected, talk together, alone.

But, the "random" text is always the same, each time i launch the program. I don't understand why.
i have always "coucou", "ça baigne?", "c'est ...", "aurevoir"

Here is, three elements on my form :

  • 1 Timer (TimerForum)
  • 1 Label (Label)
  • 1 Label (Forlab)

Here is my script :
`

extends Control

var forumcnv = 0

func _ready():
	pass

func _on_TimerForum_timeout():
	# -------------------------------------------------
	forumcnv += 1
	var reponse = String("defaut")
	# -------------------------------------------------
	var zeforum_01 = [	"bonjour",
			"coucou",
			"hello",
			"salut"]
	var zeforum_02 = [	"ça va?",
			"ça roule?",
			"ça baigne?",
			"ça flotte?"]
	var zeforum_03 = [	"c'est bien",
			"c'est cool",
			"c'est top",
			"c'est ..."]
	var zeforum_04 = [	"aurevoir",
			"à bientôt",
			"à demain",
			"a++"]
	# -------------------------------------------------
	if $"Forlab".text == String(5):
		reponse = zeforum_01[randi()%zeforum_01.size()]
		$"Label".text = reponse
	if $"Forlab".text == String(10):
		reponse = zeforum_02[randi()%zeforum_02.size()]
		$"Label".text = reponse
	if $"Forlab".text == String(15):
		reponse = zeforum_03[randi()%zeforum_03.size()]
		$"Label".text = reponse
	if $"Forlab".text == String(20):
		reponse = zeforum_04[randi()%zeforum_04.size()]
		$"Label".text = reponse
	# -------------------------------------------------
	print(forumcnv)	
	$"Forlab".text = String(forumcnv)
`

So my question is, how to make a "real" random text, please? 8D
Thanks for your advices, have a nice day 😉

Alain

  • That's on purpose.

    It is a pseudo-random generator that produces a reproducible set of random numbers. Reproducible is the keyword here. In order to get a different set of random numbers you must 'seed' the generator with a number, for instance the current time, or a hash of some sorts.

    You can seed it at the start of the program, or at any time you need it. Seeding it with the same number will result in the same sequence of pseudo-random numbers for subsequent calls to the rand() function using the generator.

    Here's detailed help:
    https://docs.godotengine.org/en/latest/tutorials/math/random_number_generation.html

    Edit: note that a "real random number" does not exist on a computer. A random number generator simulates a predefined distribution over a range of numbers, e.g. equal like rolling a single dice, or Gaussian like rolling many dice and summing the eyes, or others built into the generator. Should a computer exhibit "random" behaviour, then there is something wrong :-)

That's on purpose.

It is a pseudo-random generator that produces a reproducible set of random numbers. Reproducible is the keyword here. In order to get a different set of random numbers you must 'seed' the generator with a number, for instance the current time, or a hash of some sorts.

You can seed it at the start of the program, or at any time you need it. Seeding it with the same number will result in the same sequence of pseudo-random numbers for subsequent calls to the rand() function using the generator.

Here's detailed help:
https://docs.godotengine.org/en/latest/tutorials/math/random_number_generation.html

Edit: note that a "real random number" does not exist on a computer. A random number generator simulates a predefined distribution over a range of numbers, e.g. equal like rolling a single dice, or Gaussian like rolling many dice and summing the eyes, or others built into the generator. Should a computer exhibit "random" behaviour, then there is something wrong :-)

Well randomness cannot be generated in software, but there are some advanced hardware devices that can generate real random numbers.

I've also seen research of sampling hardware statistics in software to provide some randomness (for example, sampling the heat of the CPU/GPU with sufficient accuracy) but I'm not sure how reliable that is.

But, in general, you are correct. Or at least in terms of game programming, it will all be pseudo-random. The only need for hardware like above is for some serious scientific or security applications.

@cybereality : I guess they simply forgot to seed or randomize() it. A pseudo RNG is enough for a game, and even more. I mean, given a distribution the language built in generator (which is probably one of the C++ STL ones, or an own brew) not "more" random than the hardware generator which takes the seed for its algorithm from ... somewhere. C++ has a bunch of generators,

Btw., reproducibility is not (necessarily) an unwanted side effect.

I could spend hours going down those rabbit holes:
https://cplusplus.com/reference/random/

The hardware is not an algorithm, it uses electrical noise to generate numbers, which is actually random.

The TrueRNG Hardware Random Number Generator uses the avalanche effect in a semiconductor junction to generate true random numbers. The avalanche effect has long been used for generation of random number / noise and is a time-tested and proven random noise source. The semiconductor junction is biased to 12 volts using a boost voltage regulator (since USB only supplies 5V), amplified, then digitized at high-speed. The digitized data is selected and whitened internal to the TrueRNG and sent over the USB port with more than 400 kilobits/second of throughput.

You can read more here (and, yes, totally overkill for a game).
https://ubld.it/truerng_v3

Also, the pseudo-random number lists used the the standard library still had to be generated at one point, likely with a device like that, or a similar process. So, yes, in a way the numbers were still random (at the exact moment they were generated) but when using them it is simply cycling through a very long array.

There isn't a generated list as such, it's just a mathematical algorithm.
For example Godot uses PCG32. Storing that as an array would take 64 exabytes.
But effectively you can think of it that way, a huge array of numbers in a fixed order, the seed controls where in the array you start.

    Yeah, didn't want to start another smart ass battle :-) though it can be quite enlightening.

    Btw.: Following the link to the C++ STL random library, there is also a (albeit implementation dependant) generator for a uniform distribution that's non deterministic. Apart from that, the STL has a bunch of generators one can choose from. And searching one finds a lot of bitshifters that come with noise- or ray-tracing implementations or just for the lulz.

    https://cplusplus.com/reference/random/random_device/

    Back on topic, OP could let us know if the seeding of the generator as described in the documentation solved their problem.

    Kojack There isn't a generated list as such, it's just a mathematical algorithm.

    Oh, yeah. You're right. But effectively it still can be treated as an array, because the sequence is reproducible.

    Sure, one can treat it as an array. But that would be very slow and there'd be an infinite(*) array for every seed, and that for every deterministic generator. So something to ponder for a real Vulcan :-)

    Reproducibility doesn't matter for drawing one number, or 10 or 42, or a million. Didn't watch all of the video, so I may just repeat what was said. What matters is the probability of drawing a number in each turn, and how that probability is distributed over the range of possible numbers. If that is well enough (pretty heavy math behind that) with no repetitions or holes or some that are so far away in the sequence that it can be be dubbed (pseudo-)random.

    If you always draw a 7 then you could have used const unsigned int instead of a generator, but you want an equal distribution, or a Gaussian distribution or whatever function you want to lay over the range (C++ can be really cool).

    The reproducibility though is (as you all probably know) an important ingredient in some applications. Apart from that, for the above case, it is uninteresting, as it can be overridden with choosing a different seed any time the program starts. Or the same seed to get the same sequence of results again.

    (*) practically spoken, I mean. The Mersenne twister has a period of 2¹⁹⁹³⁷-1. That's a bit unwieldy for an array ... :-)

    Thanks for all theses answers 😃, and thank you Pixophir, i have been on your link and now i have the solution. I add "randomize()" in function çready and it works. It's ok for me for my project 😉

    Here is the script test

    extends Control
    
    var forumcnv = 0
    
    func _ready():
    	randomize()
    							
    func _on_TimerForum_timeout():
    	forumcnv += 1
    	var reponse = String("defaut")
    	var zeforum_01 = [	"bonjour",
    						"coucou",
    						"hello",
    						"salut"]
    	var zeforum_02 = [	"ça va?",
    						"ça roule?",
    						"ça baigne?",
    						"ça flotte?"]
    
    if $"Forlab".text == String(5):
    	reponse = zeforum_01[randi()%zeforum_01.size()]
    	$"Label".text = reponse
    if $"Forlab".text == String(10):
    	reponse = zeforum_02[randi()%zeforum_02.size()]
    	$"Label".text = reponse
    
    print(forumcnv)	
    $"Forlab".text = String(forumcnv)