-
Notifications
You must be signed in to change notification settings - Fork 51
/
java.cpp
43 lines (32 loc) · 799 Bytes
/
java.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
/** Copyright (c) 2022, EtlamGit */
/* This is basically a reimplementation of parts of JAVA Random class */
#include "java.h"
#include <stdexcept>
namespace Java {
Random::Random(long long seed)
{
this->seed = (seed ^ 0x5DEECE66DLL) & ((1LL << 48) - 1);
}
int Random::nextInt()
{
return next(32);
}
int Random::nextInt(int n)
{
if (n <= 0)
throw std::invalid_argument("n must be positive");
if ((n & -n) == n) // i.e., n is a power of 2
return (int) ((n * (long long) next(31)) >> 31);
int bits, val;
do {
bits = next(31);
val = bits % n;
} while (bits - val + (n - 1) < 0);
return val;
}
int Random::next(int bits)
{
seed = (seed * 0x5DEECE66DLL + 0xBLL) & ((1LL << 48) - 1);
return (int) (static_cast<unsigned long long>(seed) >> (48 - bits));
}
}