Skip to content

Replace random_shuffle with std::shuffle #5463

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

Merged
merged 3 commits into from
Jul 19, 2025

Conversation

nyllet
Copy link
Contributor

@nyllet nyllet commented Jun 24, 2025

Agreement

By creating a pull request in stk-code, you hereby agree to dual-license your contribution as
GNU General Public License version 3 or any later version and
Mozilla Public License version 2 or any later version.

This includes your previous contribution(s) under the same name of contributor.

Keep the above statement in the pull request comment for agreement.

@nyllet
Copy link
Contributor Author

nyllet commented Jun 24, 2025

std::random_shuffle has been deprecated since c++14. this is my attempt at replacing it

return generator;
}

unsigned int RandomGenerator::m_random_value = RandomGenerator::default_seed;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the purpose of this line here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's initialization of m_random_value since it's static. however, i realized that m_random_value = default_seed in the constructor was not necessary so i removed it. i then replaced the ctor with a default. new commit pushed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe i misunderstood the question. do you mean what the default_seed is used for? only to check if a seed was provided from the command line or not. if no seed is provided, the random generator will be initialized like this: std::mt19937{std::random_device{}()}, so the default_seed will never actually be used as a seed

@deveee
Copy link
Member

deveee commented Jul 3, 2025

I don't know this code so maybe it doesn't matter, just note that any changes in random generator may affect network multiplayer.

@nyllet
Copy link
Contributor Author

nyllet commented Jul 6, 2025

regarding the comment from @deveee regarding network multiplayer, i don't think it is affected yet. there are hints in this comment in random_generator.hpp: "// This generator is (currently) not good enough, i.e. it often gives
// long sequences of same numbers. And the seeding is not done (the
// mid term goal is to synchronise all random number generators on
// client and server to make less communication necessary).
// For now: just use standard random numbers:"
that someone thinks random_generator should be improved and used in network play in the future, but i don't think that is the case yet. i searched for random_generator in the files under src/network and the only hit was that random_generator.hpp is included from server_lobby.cpp, but it is not used. i also think that the important part if we want to have the same random number sequence in all clients and server in network game is the synchronization, not how the numbers are generated. i have not touched code related to synchronization as far as i understand at least

@Alayan-stk-2
Copy link
Collaborator

Alayan-stk-2 commented Jul 19, 2025

How the numbers are generated matters for networked play, because the client may not get the authoritative data from the server for 50-200ms depending on ping.

However, RNG for powerups picked from gift boxes uses a special system, so as to make client-side prediction effective. The relevant code can be seen from here on:

// In order to increase the probability of correct client prediction

I also had concerns about potential compatibility issues, but from what I can tell most uses from this random generator are for things such as giving a random track when picking the random track button. The one exception is picking nodes to spawn tire karts in 3-strikes battle, but online is FFA instead of 3 strikes anyways.

@Alayan-stk-2 Alayan-stk-2 merged commit ce9927f into supertuxkart:master Jul 19, 2025
21 checks passed
@Alayan-stk-2 Alayan-stk-2 added this to the 1.5 milestone Jul 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants