replace rand() and srand(). fix #390

This commit is contained in:
2024-08-07 13:14:56 -05:00
committed by Celtic Minstrel
parent a1022aa23b
commit fc12092a1b
6 changed files with 16 additions and 9 deletions

View File

@@ -44,11 +44,10 @@ void (*cDialog::redraw_everything)() = nullptr;
std::string cDialog::generateRandomString(){
// Not bothering to seed, because it doesn't actually matter if it's truly random.
// Though, this will be called after srand() is called in main() anyway.
int n_chars = rand() % 100;
int n_chars = ui_rand() % 100;
std::string s = "$";
while(n_chars > 0){
s += char(rand() % 96) + ' '; // was 223 ...
s += char(ui_rand() % 96) + ' '; // was 223 ...
n_chars--;
}
return s;

View File

@@ -343,16 +343,16 @@ void init_boe(int argc, char* argv[]) {
Element& srand_element = pop_next_action("srand");
std::string ts(srand_element.GetText());
srand(atoi(ts.c_str()));
game_rand.seed(atoi(ts.c_str()));
} else {
auto t = time(nullptr);
if (recording) {
std::string ts = boost::lexical_cast<std::string>(t);
record_action("srand", ts);
}
srand(t);
game_rand.seed(t);
}
std::cout << rand() << std::endl;
std::cout << game_rand() << std::endl;
init_screen_locs();
init_startup();
flushingInput = true;

View File

@@ -9,6 +9,9 @@
#include <cstdlib>
#include "mathutil.hpp"
std::mt19937 game_rand;
std::mt19937 ui_rand;
short get_ran (short times,short min,short max){
long int store;
short to_ret = 0;
@@ -17,7 +20,7 @@ short get_ran (short times,short min,short max){
if(max == min) return times * min;
for(short i = 1; i < times + 1; i++) {
store = rand();
store = game_rand();
to_ret += min + (store % (max - min + 1));
}
return to_ret;

View File

@@ -10,6 +10,7 @@
#include <cmath>
#include <SFML/System/Time.hpp>
#include <random>
// Make sure min and max macros are not defined.
// Some Windows headers may define these.
@@ -21,6 +22,10 @@
#endif
using std::abs;
extern std::mt19937 game_rand;
extern std::mt19937 ui_rand;
short get_ran(short times, short min, short max);
short max(short a,short b);
short min(short a,short b);

View File

@@ -114,7 +114,7 @@ int main(int argc, char* argv[]) {
#endif
check_for_intel();
srand(time(nullptr));
game_rand.seed(time(nullptr));
set_up_apple_events();
process_args(argc, argv);

View File

@@ -218,7 +218,7 @@ void init_scened(int argc, char* argv[]) {
set_cursor(watch_curs);
check_for_intel();
srand(time(nullptr));
game_rand.seed(time(nullptr));
cen_x = 18;
cen_y = 18;