-
Notifications
You must be signed in to change notification settings - Fork 81
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
Make AIJH Actions Reproducible by Using the Games Random Seed #1739
base: master
Are you sure you want to change the base?
Changes from all commits
7f20e8d
244e436
b5f6244
cdf19fc
9c698e3
0ceff0d
41864ab
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Copyright (C) 2005 - 2025 Settlers Freaks (sf-team at siedler25.org) | ||
// | ||
// SPDX-License-Identifier: GPL-2.0-or-later | ||
|
||
#include "ai/random.h" | ||
|
||
namespace AI { | ||
|
||
std::minstd_rand& getRandomGenerator() | ||
{ | ||
static std::minstd_rand rng(std::random_device{}()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. are the std rngs defined to have the same randomeness between platforms now? Since for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The rng yes. There might be differences in the distribution implementations leading to slightly different results or something like that. But IMO we don't really need that here: It is reproducible when run on the same platform which is enough for the current use case, isn't it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In my understanding There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Correct. It is just the distribution classes which are implementation defined. Found a SO post confirming that. But still yes: Fine for this use case. |
||
return rng; | ||
} | ||
|
||
} // namespace AI |
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,48 @@ | ||||||||||||||||
// Copyright (C) 2005 - 2025 Settlers Freaks (sf-team at siedler25.org) | ||||||||||||||||
// | ||||||||||||||||
// SPDX-License-Identifier: GPL-2.0-or-later | ||||||||||||||||
|
||||||||||||||||
#pragma once | ||||||||||||||||
|
||||||||||||||||
#include "helpers/random.h" | ||||||||||||||||
#include <random> | ||||||||||||||||
|
||||||||||||||||
namespace AI { | ||||||||||||||||
|
||||||||||||||||
std::minstd_rand& getRandomGenerator(); | ||||||||||||||||
|
||||||||||||||||
// Return a random value (min and max are included) | ||||||||||||||||
template<typename T> | ||||||||||||||||
T randomValue(T min = std::numeric_limits<T>::min(), T max = std::numeric_limits<T>::max()) | ||||||||||||||||
wichern marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||
{ | ||||||||||||||||
return helpers::randomValue(getRandomGenerator(), min, max); | ||||||||||||||||
Flamefire marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
// Return a random bool: | ||||||||||||||||
// random() ... will return true|false with 50% chance each | ||||||||||||||||
// random(15) ... will return true in 1/15 of the cases | ||||||||||||||||
Flamefire marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||
// random(20, 5) ... will return true in 5 out of 20 cases, i.e. a probability of 25%. Sames as random(4, 1) | ||||||||||||||||
inline bool random(unsigned total = 2u, unsigned chance = 1u) | ||||||||||||||||
{ | ||||||||||||||||
RTTR_Assert(total > 0u); | ||||||||||||||||
return (chance >= total) || randomValue(1u, total) <= chance; | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
template<typename ContainerT> | ||||||||||||||||
inline unsigned randomIndex(const ContainerT& container) | ||||||||||||||||
{ | ||||||||||||||||
RTTR_Assert(!container.empty()); | ||||||||||||||||
return randomValue(0u, static_cast<unsigned>(container.size()) - 1u); | ||||||||||||||||
Comment on lines
+32
to
+35
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have just seen we already have this function in helpers:
Suggested change
(template functions don't need to be marked inline) There is also |
||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
template<typename ContainerT> | ||||||||||||||||
inline auto randomElement(const ContainerT& container) | ||||||||||||||||
{ | ||||||||||||||||
RTTR_Assert(!container.empty()); | ||||||||||||||||
auto it = container.begin(); | ||||||||||||||||
if(container.size() > 1u) | ||||||||||||||||
std::advance(it, randomIndex(container)); | ||||||||||||||||
return *it; | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
} // namespace AI |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this be random(10u)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. See my previous comment which includes a "recipe" for translating those. Didn't want to annotate all of them to avoid flooding this PR too much ;-)