Skip to content

Commit 3ecabae

Browse files
committed
Replace more rand() % NUM by randranges
1 parent efee1db commit 3ecabae

File tree

7 files changed

+19
-19
lines changed

7 files changed

+19
-19
lines changed

src/test/coins_tests.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ BOOST_AUTO_TEST_CASE(coins_cache_simulation_test)
145145
for (unsigned int i = 0; i < NUM_SIMULATION_ITERATIONS; i++) {
146146
// Do a random modification.
147147
{
148-
uint256 txid = txids[insecure_rand() % txids.size()]; // txid we're going to modify in this iteration.
148+
uint256 txid = txids[insecure_randrange(txids.size())]; // txid we're going to modify in this iteration.
149149
Coin& coin = result[COutPoint(txid, 0)];
150150
const Coin& entry = (insecure_randrange(500) == 0) ? AccessByTxid(*stack.back(), txid) : stack.back()->AccessCoin(COutPoint(txid, 0));
151151
BOOST_CHECK(coin == entry);
@@ -201,7 +201,7 @@ BOOST_AUTO_TEST_CASE(coins_cache_simulation_test)
201201
if (insecure_randrange(100) == 0) {
202202
// Every 100 iterations, flush an intermediate cache
203203
if (stack.size() > 1 && insecure_randrange(2) == 0) {
204-
unsigned int flushIndex = insecure_rand() % (stack.size() - 1);
204+
unsigned int flushIndex = insecure_randrange(stack.size() - 1);
205205
stack[flushIndex]->Flush();
206206
}
207207
}
@@ -434,7 +434,7 @@ BOOST_AUTO_TEST_CASE(updatecoins_simulation_test)
434434
if (insecure_randrange(100) == 0) {
435435
// Every 100 iterations, flush an intermediate cache
436436
if (stack.size() > 1 && insecure_randrange(2) == 0) {
437-
unsigned int flushIndex = insecure_rand() % (stack.size() - 1);
437+
unsigned int flushIndex = insecure_randrange(stack.size() - 1);
438438
stack[flushIndex]->Flush();
439439
}
440440
}

src/test/crypto_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ void TestVector(const Hasher &h, const In &in, const Out &out) {
3838
Hasher hasher(h);
3939
size_t pos = 0;
4040
while (pos < in.size()) {
41-
size_t len = insecure_rand() % ((in.size() - pos + 1) / 2 + 1);
41+
size_t len = insecure_randrange((in.size() - pos + 1) / 2 + 1);
4242
hasher.Write((unsigned char*)&in[pos], len);
4343
pos += len;
4444
if (pos > 0 && pos + 2 * out.size() > in.size() && pos < in.size()) {

src/test/merkle_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ BOOST_AUTO_TEST_CASE(merkle_test)
120120
// If ntx <= 16, try all branches. Otherwise, try 16 random ones.
121121
int mtx = loop;
122122
if (ntx > 16) {
123-
mtx = insecure_rand() % ntx;
123+
mtx = insecure_randrange(ntx);
124124
}
125125
std::vector<uint256> newBranch = BlockMerkleBranch(block, mtx);
126126
std::vector<uint256> oldBranch = BlockGetMerkleBranch(block, merkleTree, mtx);

src/test/pmt_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class CPartialMerkleTreeTester : public CPartialMerkleTree
2121
public:
2222
// flip one bit in one of the hashes - this should break the authentication
2323
void Damage() {
24-
unsigned int n = insecure_rand() % vHash.size();
24+
unsigned int n = insecure_randrange(vHash.size());
2525
int bit = insecure_randrange(256);
2626
*(vHash[n].begin() + (bit>>3)) ^= 1<<(bit&7);
2727
}

src/test/prevector_tests.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -200,21 +200,21 @@ BOOST_AUTO_TEST_CASE(PrevectorTestInt)
200200
for (int i = 0; i < 2048; i++) {
201201
int r = insecure_rand();
202202
if ((r % 4) == 0) {
203-
test.insert(insecure_rand() % (test.size() + 1), insecure_rand());
203+
test.insert(insecure_randrange(test.size() + 1), insecure_rand());
204204
}
205205
if (test.size() > 0 && ((r >> 2) % 4) == 1) {
206-
test.erase(insecure_rand() % test.size());
206+
test.erase(insecure_randrange(test.size()));
207207
}
208208
if (((r >> 4) % 8) == 2) {
209209
int new_size = std::max<int>(0, std::min<int>(30, test.size() + (insecure_randrange(5)) - 2));
210210
test.resize(new_size);
211211
}
212212
if (((r >> 7) % 8) == 3) {
213-
test.insert(insecure_rand() % (test.size() + 1), 1 + (insecure_randrange(2)), insecure_rand());
213+
test.insert(insecure_randrange(test.size() + 1), 1 + insecure_randrange(2), insecure_rand());
214214
}
215215
if (((r >> 10) % 8) == 4) {
216216
int del = std::min<int>(test.size(), 1 + (insecure_randrange(2)));
217-
int beg = insecure_rand() % (test.size() + 1 - del);
217+
int beg = insecure_randrange(test.size() + 1 - del);
218218
test.erase(beg, beg + del);
219219
}
220220
if (((r >> 13) % 16) == 5) {
@@ -229,11 +229,11 @@ BOOST_AUTO_TEST_CASE(PrevectorTestInt)
229229
for (int k = 0; k < num; k++) {
230230
values[k] = insecure_rand();
231231
}
232-
test.insert_range(insecure_rand() % (test.size() + 1), values, values + num);
232+
test.insert_range(insecure_randrange(test.size() + 1), values, values + num);
233233
}
234234
if (((r >> 26) % 32) == 8) {
235235
int del = std::min<int>(test.size(), 1 + (insecure_randrange(4)));
236-
int beg = insecure_rand() % (test.size() + 1 - del);
236+
int beg = insecure_randrange(test.size() + 1 - del);
237237
test.erase(beg, beg + del);
238238
}
239239
r = insecure_rand();
@@ -244,7 +244,7 @@ BOOST_AUTO_TEST_CASE(PrevectorTestInt)
244244
test.shrink_to_fit();
245245
}
246246
if (test.size() > 0) {
247-
test.update(insecure_rand() % test.size(), insecure_rand());
247+
test.update(insecure_randrange(test.size()), insecure_rand());
248248
}
249249
if (((r >> 11) % 1024) == 11) {
250250
test.clear();

src/test/sighash_tests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ void static RandomScript(CScript &script) {
9191
script = CScript();
9292
int ops = (insecure_randrange(10));
9393
for (int i=0; i<ops; i++)
94-
script << oplist[insecure_rand() % (sizeof(oplist)/sizeof(oplist[0]))];
94+
script << oplist[insecure_randrange(sizeof(oplist)/sizeof(oplist[0]))];
9595
}
9696

9797
void static RandomTransaction(CMutableTransaction &tx, bool fSingle) {
@@ -138,7 +138,7 @@ BOOST_AUTO_TEST_CASE(sighash_test)
138138
RandomTransaction(txTo, (nHashType & 0x1f) == SIGHASH_SINGLE);
139139
CScript scriptCode;
140140
RandomScript(scriptCode);
141-
int nIn = insecure_rand() % txTo.vin.size();
141+
int nIn = insecure_randrange(txTo.vin.size());
142142

143143
uint256 sh, sho;
144144
sho = SignatureHashOld(scriptCode, txTo, nIn, nHashType);

src/test/skiplist_tests.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ BOOST_AUTO_TEST_CASE(skiplist_test)
3434
}
3535

3636
for (int i=0; i < 1000; i++) {
37-
int from = insecure_rand() % (SKIPLIST_LENGTH - 1);
38-
int to = insecure_rand() % (from + 1);
37+
int from = insecure_randrange(SKIPLIST_LENGTH - 1);
38+
int to = insecure_randrange(from + 1);
3939

4040
BOOST_CHECK(vIndex[SKIPLIST_LENGTH - 1].GetAncestor(from) == &vIndex[from]);
4141
BOOST_CHECK(vIndex[from].GetAncestor(to) == &vIndex[to]);
@@ -115,7 +115,7 @@ BOOST_AUTO_TEST_CASE(findearliestatleast_test)
115115
} else {
116116
// randomly choose something in the range [MTP, MTP*2]
117117
int64_t medianTimePast = vBlocksMain[i].GetMedianTimePast();
118-
int r = insecure_rand() % medianTimePast;
118+
int r = insecure_randrange(medianTimePast);
119119
vBlocksMain[i].nTime = r + medianTimePast;
120120
vBlocksMain[i].nTimeMax = std::max(vBlocksMain[i].nTime, vBlocksMain[i-1].nTimeMax);
121121
}
@@ -134,7 +134,7 @@ BOOST_AUTO_TEST_CASE(findearliestatleast_test)
134134
// Verify that FindEarliestAtLeast is correct.
135135
for (unsigned int i=0; i<10000; ++i) {
136136
// Pick a random element in vBlocksMain.
137-
int r = insecure_rand() % vBlocksMain.size();
137+
int r = insecure_randrange(vBlocksMain.size());
138138
int64_t test_time = vBlocksMain[r].nTime;
139139
CBlockIndex *ret = chain.FindEarliestAtLeast(test_time);
140140
BOOST_CHECK(ret->nTimeMax >= test_time);

0 commit comments

Comments
 (0)