Remove C-style 'srand()' and use our already existing 'Random' class.

This also fixes a warning of NULL vs. nullptr.
This commit is contained in:
Stephen Anthony 2021-10-09 20:22:59 -02:30
parent e2f7712d06
commit 5d64f0a9c3
2 changed files with 16 additions and 9 deletions

View File

@ -516,16 +516,15 @@ string OSystem::createConsole(const FilesystemNode& rom, const string& md5sum,
// Make sure there always is an id
if(settings().getString("plusroms.id") == EmptyString)
{
const int ID_LEN = 32 - 2; // WE prefix added later
constexpr int ID_LEN = 32 - 2; // WE prefix added later
const char* HEX_DIGITS = "0123456789ABCDEF";
char id_chr[ID_LEN] = {0};
srand(time(NULL));
for(int i = 0; i < ID_LEN; i++)
id_chr[i] = HEX_DIGITS[(rand() % 16)];
Random rnd;
for(char& c: id_chr)
c = HEX_DIGITS[rnd.next() % 16];
std::string id_str(id_chr, ID_LEN);
settings().setValue("plusroms.id", id_str);
settings().setValue("plusroms.id", string(id_chr, ID_LEN));
}
myEventHandler->changeStateByEvent(Event::PlusRomsSetupMode);

View File

@ -18,6 +18,8 @@
#ifndef RANDOM_HXX
#define RANDOM_HXX
#include <chrono>
#include "bspf.hxx"
#include "Serializable.hxx"
@ -32,7 +34,14 @@ class Random : public Serializable
{
public:
/**
Create a new random number generator
Create a new random number generator with seed based on system time.
*/
explicit Random() {
initSeed(std::chrono::system_clock::now().time_since_epoch().count());
}
/**
Create a new random number generator with given seed.
*/
explicit Random(uInt32 seed) { initSeed(seed); }
@ -46,7 +55,7 @@ class Random : public Serializable
}
/**
Answer the next random number from the random number generator
Answer the next random number from the random number generator.
@return A random number
*/
@ -107,7 +116,6 @@ class Random : public Serializable
private:
// Following constructors and assignment operators not supported
Random() = delete;
Random(const Random&) = delete;
Random(Random&&) = delete;
Random& operator=(const Random&) = delete;