Skip to content

Commit

Permalink
Make shuffle deterministic
Browse files Browse the repository at this point in the history
  • Loading branch information
SupSuper committed Oct 14, 2020
1 parent da95e73 commit 02bac59
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 15 deletions.
11 changes: 0 additions & 11 deletions src/Engine/RNG.cpp
Expand Up @@ -152,17 +152,6 @@ bool percent(int value)
return (generate(0, 99) < value);
}

/**
* Generates a random positive integer up to a number.
* @param max Maximum number, exclusive.
* @return Generated number.
*/
int generateEx(int max)
{
uint64_t num = next();
return (int)(num % max);
}

}

}
10 changes: 6 additions & 4 deletions src/Engine/RNG.h
Expand Up @@ -18,6 +18,7 @@
* along with OpenXcom. If not, see <http://www.gnu.org/licenses/>.
*/
#include <algorithm>
#include <vector>
#define __STDC_LIMIT_MACROS
#include <stdint.h>

Expand Down Expand Up @@ -45,17 +46,18 @@ namespace RNG
double boxMuller(double m = 0, double s = 1);
/// Generates a percentage chance.
bool percent(int value);
/// Generates a random integer number, exclusive.
int generateEx(int max);
/// Shuffles a list randomly.
/**
* Randomly changes the orders of the elements in a list.
* @param list The container to randomize.
*/
template <typename T>
void shuffle(T &list)
void shuffle(std::vector<T> &list)
{
std::random_shuffle(list.begin(), list.end(), generateEx);
if (list.empty())
return;
for (size_t i = list.size() - 1; i > 0; --i)
std::swap(list[i], list[generate(0, i)]);
}
}

Expand Down

0 comments on commit 02bac59

Please sign in to comment.