myrandom

Posted by marginal on 2021-08-09
Estimated Reading Time 4 Minutes
Words 735 In Total
Viewed Times

一个随机数脚本, android 是java的

在网上翻翻找找总结了一下

python

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
"""
from ctfbox.crypto import crypto
crypto.android_random(2) #seed must be set
crypto.windows_srand(2) #seed must be set
crypto.linux_srand(2) #seed must be set
for i in range(50):
print(crypto.android_nextInt()) #random number
print(crypto.android_nextInt_bound(10)) #random numbers between[0,10)
print(crypto.windows_rand()) #random number
print(crypto.linux_rand()) #random number
"""

from ctypes import c_int
windows_status = 1

def windows_srand(seed):
"""
Args:
seed(int): Random number seed

Returns: void

Example:
windows_srand(1)

"""
global windows_status
windows_status = seed

def windows_rand():
"""
Returns:
int: Random numbers

Example:
#seed must be set
windows_rand()
"""
global windows_status
windows_status = (214013*windows_status+2531011) & 0xffffffff
return windows_status >> 16 & ((1 << 15)-1)


android_seed = 0
android_multiplier = 0x5DEECE66D
android_addend = 0xB
android_mask = (1 << 48) - 1
android_seedUniquifier = 8682522807148012

def android_srand(seed):
"""
Args:
seed(int): Random numbers seed
Returns: void

Example:
android_srand(1)

"""
global android_seed
android_seed = _initialScramble(seed)

def _initialScramble(seed):
return (seed ^ android_multiplier) & android_mask

def _next(bits):
global android_seed
oldseed = 0
nextseed = 0
seed = android_seed
oldseed = seed
nextseed = (oldseed * android_multiplier + android_addend) & android_mask
android_seed = nextseed
return c_int(((nextseed >> (48 - bits)))).value

def android_nextInt():
"""
Returns:
int: Random numbers

Example:
#seed must be set
android_nextInt()
"""
return _next(32)

def android_nextInt_bound(bound):
"""
Args:
bound(int): Random numbers upper limit

Returns:
int: Random numbers between[0, bound)

Example:
#seed must be set
android_nextInt_bound(10)

"""
global android_seed
r = _next(31)
m = bound - 1
if bound & m == 0:
r = (((bound * (r & 0xffffffffffffffff)) >> 31) & 0xffffffff)
else:
u = r
r = u % bound
while u - r + m < 0:
r = u % bound
u = _next(31)
return r


linux_status = 0
linux_r = []
def linux_srand(seed):
"""
Args:
seed(int): Random numbers seed

Returns: void

Example:
linux_srand(1)

"""
if seed == 0:
seed = 1
word = seed
seed = seed & 0xffffffff
global linux_status
global linux_r
linux_status = 0
linux_r = [0] * (344 + linux_status)
linux_r[0] = seed
for i in range(1, 31):
if (word < 0):
hi = (-word) // 127773
hi = -hi
lo = (-word) % 127773
lo = -lo
else:
hi = word // 127773
lo = word % 127773
word = ((16807 * lo)) - ((2836 * hi))
if word < 0:
word = (2147483647 + word) & 0xffffffff
linux_r[i] = word
for i in range(31, 34):
linux_r[i] = linux_r[i - 31]
for i in range(34, 344):
linux_r[i] = (((linux_r[i - 31] + linux_r[i - 3]) & 0xffffffff) % (1 << 32)) & 0xffffffff

def linux_rand():
"""
Returns:
int: Random numbers

Example:
#seed must be set
linux_rand()
"""
global linux_status
global linux_r
linux_r.append(0)
linux_r[344 + linux_status] = (((linux_r[344 + linux_status - 31] + linux_r[344 + linux_status - 3]) & 0xffffffff) % (1 << 32)) & 0xffffffff
linux_status += 1
return linux_r[344 + linux_status - 1] >> 1

C语言

linux

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
include<iostream>

using namespace std;

int main()
{
const int SEED = 1;
const unsigned int MOD = 0x7ffffffff;
const long long MOD_MAX = (1LL << 32);
unsigned int r[700];
r[0] = 2; //种子
int32_t word;
word = seed;
for (int i = 1; i < 31; i++)
{
long int hi = word / 127773;
long int lo = word % 127773;
word = 16807 * lo - 2836 * hi;
if (word < 0)
word += 2147483647;
r[i] = word;
}
for(int i = 31; i < 34; i++)
r[i] = r[i - 31];
for(int i = 34; i < 700; i++) {
r[i] = (r[i - 3] + r[i - 31]) % MOD_MAX;
}
for (int i = 344; i < 644; i++) //r[344]为第一个随机数,之后为随机数列,要得到更多的随机数,加长数组长度和循环次数就行
cout << (r[i] >> 1) << endl;
system("pause");
}

windows

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
#include<iostream>
#include<cstdlib>

# using namespace std;

# unsigned int status=1;
# int seed = 1;

# int my_srand(unsigned int seed){
# status=seed;
# }

# int my_rand(){
# status=214013*status+2531011;
# return status>>16&((1<<15)-1);
# }
# int main()
# {
# int n = 1000;
# srand(1);
# while (n--)
# {
# cout << rand() << " " << my_rand() << endl;
# }
# system("pause");
# }

如果您喜欢此博客或发现它对您有用,则欢迎对此发表评论。 也欢迎您共享此博客,以便更多人可以参与。 如果博客中使用的图像侵犯了您的版权,请与作者联系以将其删除。 谢谢 !