godotengine / godot

Godot Engine – Multi-platform 2D and 3D game engine
https://godotengine.org
MIT License
91.05k stars 21.18k forks source link

array.shuffle() does not work properly #28689

Closed elieobeid7 closed 5 years ago

elieobeid7 commented 5 years ago

https://godot.readthedocs.io/uk/latest/classes/class_array.html#class-array-method-shuffle

if you use that, you restarted your game, from the editor, you will have the same values over and over again.

clayjohn commented 5 years ago

That is expected behaviour. If you want to get random values each time you run the game you should use the function randomize().

A note could be added to the docs pointing users to randomize() http://docs.godotengine.org/en/latest/classes/class_@gdscript.html#class-gdscript-method-randomize

elieobeid7 commented 5 years ago

Thanks for pointing me to randomize()

Should I close the issue now or after the docs gets updated?

By the way I tried updating the docs, I press on the button

" Edit on GitHub"

Takes me to 404 page

https://github.com/godotengine/godot-docs-l10n/blob/master/docs/classes/class_array.rst

That needs to be fixed too, either remove the button or the hyperlink should be fixed.

clayjohn commented 5 years ago

You should close it after the docs get updated.

CowThing commented 5 years ago

Should there just be a whole page in the docs describing how RNG works and how seeds/randomize work? I've seen similar issues of people reporting random things being exactly the same every time they run their game, and it's because they haven't randomized or set a seed.

elieobeid7 commented 5 years ago

By the way why not randomize the seed by default? what is the benefit of not doing that? doesn't that become predictable and thus as if one never used array.shuffle() at all? I mean what is the point of having a shuffle that can't 'shuffle'. If Something isn't random, it isn't shuffled, so why shuffle doesn't call randomize by default?

clayjohn commented 5 years ago

@CowThing That is a good idea, it would be a welcome addition. You should make an issue in the godot-docs repo and if you have time maybe put together a PR. It would be a good tutorial to put in the math section.

By the way why not randomize the seed by default?

@elieobeid7 Oftentimes what you want when using randomness is to have a different result each time the function is called rather than a different result each time the game is run. This is especially the case for games that use procedural generation. You want to reliably generate the same data each run.

I mean what is the point of having a shuffle that can't 'shuffle'. If Something isn't random, it isn't shuffled, so why shuffle doesn't call randomize by default?

It is still randomized, its just the same random sequence each run. If you run array.shuffle() multiple times in the same run it will return a different order each time.

Computers actually cannot produce 'random' numbers. All random number generation is actually psuedo random number generation. It takes a seed as input and produces a seemingly random set of results as output. This is how nearly all RNG works on computers except for high-tech cryptography algorithms and such.

isaacremnant commented 5 years ago

@clayjohn But there is the RandomNumberGenerator class now, so if you need reproducible noise for a given task, it's best to setup a local generator with a seed you control. The way I see it, the core rand functions (and Array.shuffle by extension) shouldn't be expected to be reproducible because the same generator can be accessed from everywhere (and used by stuff like particles).

Unless I'm having a giant brain fart, It would be preferable imo to randomize the core random functions on startup and have the user use their own RandomNumberGenerator when reproducible noise is desired.

Also, Array.shuffle and other functions that depend on core random functions could probably use an optional RandomNumberGenerator argument to be used instead of the core functions!

clayjohn commented 5 years ago

The way I see it, the core rand functions (and Array.shuffle by extension) shouldn't be expected to be reproducible because the same generator can be accessed from everywhere (and used by stuff like particles).

I don't see why that would be a problem. But I also have no preference for the default behaviour. I'm just stating the current behaviour. 🤷‍♂

Unless I'm having a giant brain fart, It would be preferable imo to randomize the core random functions on startup and have the user use their own RandomNumberGenerator when reproducible noise is desired.

Thats a good idea! It just isn't the way things work now. Good idea for a PR!

isaacremnant commented 5 years ago

I don't see why that would be a problem.

Should have phrased this better! I meant that any unforseen call to rand will mess your pattern (user input, threading, etc). It's only a problem if you need the pattern to be the same in all runs (think of procedurally generating a world), in which case I think you don't have a choice but to use RandomNumberGenerator.

I don't have a preference either, but the argument against randomizing on startup is good for simulation softwares, not so much for a game engine I think.

akien-mga commented 5 years ago

Fixed by #28832.