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

array.shuffle() does not work properly #28689

Closed
elieobeid7 opened this issue May 5, 2019 · 10 comments
Closed

array.shuffle() does not work properly #28689

elieobeid7 opened this issue May 5, 2019 · 10 comments

Comments

@elieobeid7
Copy link

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
Copy link
Member

clayjohn commented May 5, 2019

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/[email protected]#class-gdscript-method-randomize

@elieobeid7
Copy link
Author

elieobeid7 commented May 5, 2019

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
Copy link
Member

clayjohn commented May 5, 2019

You should close it after the docs get updated.

@CowThing
Copy link
Contributor

CowThing commented May 5, 2019

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
Copy link
Author

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
Copy link
Member

clayjohn commented May 5, 2019

@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
Copy link
Contributor

isaacremnant commented May 5, 2019

@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
Copy link
Member

clayjohn commented May 6, 2019

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
Copy link
Contributor

isaacremnant commented May 6, 2019

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
Copy link
Member

Fixed by #28832.

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

Successfully merging a pull request may close this issue.

5 participants