Commit 8799b7a1 authored by Chen Bill's avatar Chen Bill

update mt19937, signature of create_duel() API

parent 65d50ca5
......@@ -8,6 +8,7 @@
#ifndef MTRANDOM_H_
#define MTRANDOM_H_
#include <cstdint>
#include <random>
class mt19937 {
......@@ -15,30 +16,30 @@ public:
const unsigned int rand_max;
mt19937() :
rng(), rand_max((rng.max)()) {}
explicit mt19937(unsigned int seed) :
rng(seed), rand_max((rng.max)()) {}
rng(), rand_max(rng.max()) {}
explicit mt19937(uint_fast32_t seed) :
rng(seed), rand_max(rng.max()) {}
// mersenne_twister_engine
void reset(unsigned int seed) {
void reset(uint_fast32_t seed) {
rng.seed(seed);
}
unsigned int rand() {
uint_fast32_t rand() {
return rng();
}
// uniform_int_distribution
int get_random_integer(int l, int h) {
unsigned int range = (unsigned int)(h - l + 1);
unsigned int secureMax = rand_max - rand_max % range;
unsigned int x;
uint_fast32_t range = (uint_fast32_t)(h - l + 1);
uint_fast32_t secureMax = rng.max() - rng.max() % range;
uint_fast32_t x;
do {
x = rng();
} while (x >= secureMax);
return (int)(l + x % range);
return l + (int)(x % range);
}
int get_random_integer_old(int l, int h) {
int result = (int)((double)rng() / rand_max * ((double)h - l + 1)) + l;
int result = (int)((double)rng() / rng.max() * ((double)h - l + 1)) + l;
if (result > h)
result = h;
return result;
......
......@@ -57,7 +57,7 @@ uint32 default_card_reader(uint32 code, card_data* data) {
uint32 default_message_handler(void* pduel, uint32 message_type) {
return 0;
}
extern "C" DECL_DLLEXPORT intptr_t create_duel(uint32 seed) {
extern "C" DECL_DLLEXPORT intptr_t create_duel(uint_fast32_t seed) {
duel* pduel = new duel();
duel_set.insert(pduel);
pduel->random.reset(seed);
......
......@@ -35,7 +35,7 @@ byte* read_script(const char* script_name, int* len);
uint32 read_card(uint32 code, card_data* data);
uint32 handle_message(void* pduel, uint32 message_type);
extern "C" DECL_DLLEXPORT intptr_t create_duel(uint32 seed);
extern "C" DECL_DLLEXPORT intptr_t create_duel(uint_fast32_t seed);
extern "C" DECL_DLLEXPORT void start_duel(intptr_t pduel, int32 options);
extern "C" DECL_DLLEXPORT void end_duel(intptr_t pduel);
extern "C" DECL_DLLEXPORT void set_player_info(intptr_t pduel, int32 playerid, int32 lp, int32 startcount, int32 drawcount);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment