-
Notifications
You must be signed in to change notification settings - Fork 0
/
Random.cpp
43 lines (33 loc) · 970 Bytes
/
Random.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include "include/IO.h"
#include "include/Random.h"
#include "include/Converter.h"
BigInt Random::next(uint32_t byteCount)
{
if (byteCount < 0) return BigInt();
BigInt res(byteCount);
//? Random mỗi byte giá trị từ 0 đến 255
for (uint32_t i = 0; i < res.byteCount; i++)
{
res.bytes[i] = rand() % 256;
}
res = abs(res);
//io.writeLog("[Random::next] random number: ", res, BigIntBase::BASE_10);
return res;
}
BigInt Random::next(BigInt n)
{
uint32_t maxBitCount = n.getBitLength();
uint32_t resByteCount = maxBitCount / 8;
BigInt res = next(resByteCount ? resByteCount : 1);
//? Chia lấy dư để res không vượt quá n
res = res % n;
//io.writeLog("[Random::next] random number smaller than n: ", res, BigIntBase::BASE_10);
return res;
}
BigInt Random::next(BigInt a, BigInt b)
{
BigInt res = next(b - a);
res = res + a;
//io.writeLog("[Random::next] random number in [a, b): ", res, BigIntBase::BASE_10);
return res;
}