Skip to content

Commit ee2e997

Browse files
authored
Merge pull request g4klx#613 from jg1uaa/master
replace rand() -> MT19937 random number generator
2 parents 4dd3969 + dfaedb4 commit ee2e997

File tree

4 files changed

+22
-11
lines changed

4 files changed

+22
-11
lines changed

DMRNetwork.cpp

+11-7
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ m_height(0),
6666
m_location(),
6767
m_description(),
6868
m_url(),
69-
m_beacon(false)
69+
m_beacon(false),
70+
m_random()
7071
{
7172
assert(!address.empty());
7273
assert(port > 0U);
@@ -85,11 +86,13 @@ m_beacon(false)
8586
m_id[2U] = id >> 8;
8687
m_id[3U] = id >> 0;
8788

88-
CStopWatch stopWatch;
89-
::srand(stopWatch.start());
89+
std::random_device rd;
90+
std::mt19937 mt(rd());
91+
m_random = mt;
9092

91-
m_streamId[0U] = ::rand() + 1U;
92-
m_streamId[1U] = ::rand() + 1U;
93+
std::uniform_int_distribution<uint32_t> dist(0x00000001, 0xfffffffe);
94+
m_streamId[0U] = dist(m_random);
95+
m_streamId[1U] = dist(m_random);
9396
}
9497

9598
CDMRNetwork::~CDMRNetwork()
@@ -246,17 +249,18 @@ bool CDMRNetwork::write(const CDMRData& data)
246249

247250
unsigned int slotIndex = slotNo - 1U;
248251

252+
std::uniform_int_distribution<uint32_t> dist(0x00000001, 0xfffffffe);
249253
unsigned char dataType = data.getDataType();
250254
if (dataType == DT_VOICE_SYNC) {
251255
buffer[15U] |= 0x10U;
252256
} else if (dataType == DT_VOICE) {
253257
buffer[15U] |= data.getN();
254258
} else {
255259
if (dataType == DT_VOICE_LC_HEADER)
256-
m_streamId[slotIndex] = ::rand() + 1U;
260+
m_streamId[slotIndex] = dist(m_random);
257261

258262
if (dataType == DT_CSBK || dataType == DT_DATA_HEADER)
259-
m_streamId[slotIndex] = ::rand() + 1U;
263+
m_streamId[slotIndex] = dist(m_random);
260264

261265
buffer[15U] |= (0x20U | dataType);
262266
}

DMRNetwork.h

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
#include <string>
2929
#include <cstdint>
30+
#include <random>
3031

3132
class CDMRNetwork
3233
{
@@ -106,6 +107,7 @@ class CDMRNetwork
106107
std::string m_url;
107108

108109
bool m_beacon;
110+
std::mt19937 m_random;
109111

110112
bool writeLogin();
111113
bool writeAuthorisation();

DStarNetwork.cpp

+7-4
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,16 @@ m_inId(0U),
4444
m_buffer(1000U, "D-Star Network"),
4545
m_pollTimer(1000U, 60U),
4646
m_linkStatus(LS_NONE),
47-
m_linkReflector(NULL)
47+
m_linkReflector(NULL),
48+
m_random()
4849
{
4950
m_address = CUDPSocket::lookup(gatewayAddress);
5051

5152
m_linkReflector = new unsigned char[DSTAR_LONG_CALLSIGN_LENGTH];
5253

53-
CStopWatch stopWatch;
54-
::srand(stopWatch.start());
54+
std::random_device rd;
55+
std::mt19937 mt(rd());
56+
m_random = mt;
5557
}
5658

5759
CDStarNetwork::~CDStarNetwork()
@@ -85,7 +87,8 @@ bool CDStarNetwork::writeHeader(const unsigned char* header, unsigned int length
8587
buffer[4] = busy ? 0x22U : 0x20U;
8688

8789
// Create a random id for this transmission
88-
m_outId = (::rand() % 65535U) + 1U;
90+
std::uniform_int_distribution<uint16_t> dist(0x0001, 0xfffe);
91+
m_outId = dist(m_random);
8992

9093
buffer[5] = m_outId / 256U; // Unique session id
9194
buffer[6] = m_outId % 256U;

DStarNetwork.h

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
#include <cstdint>
2828
#include <string>
29+
#include <random>
2930

3031
class CDStarNetwork {
3132
public:
@@ -64,6 +65,7 @@ class CDStarNetwork {
6465
CTimer m_pollTimer;
6566
LINK_STATUS m_linkStatus;
6667
unsigned char* m_linkReflector;
68+
std::mt19937 m_random;
6769

6870
bool writePoll(const char* text);
6971
};

0 commit comments

Comments
 (0)