Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f3d7ef6

Browse files
Get sample_invert working
1 parent 12cab19 commit f3d7ef6

File tree

1 file changed

+47
-98
lines changed

1 file changed

+47
-98
lines changed

‎main.py‎

Lines changed: 47 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ def sample_seed(seed, i):
77
Int32MinValue = -2147483648
88
# same as 5 but last loops unwound
99
if seed == Int32MinValue:
10-
subtraction = Int32MinValue
10+
subtraction = MBIG
1111
else:
1212
subtraction = abs(seed)
1313

@@ -109,45 +109,45 @@ def sample_seed(seed, i):
109109
return (add + mul * subtraction) % MBIG
110110

111111
# invert prng, ie: invert_sample(sample_seed(seed, n), n) == x
112-
# def invert_sample(rand, i):
113-
# ret_nums = [
114-
# (1559595546, 1121899819),
115-
# (1755192844, 630111683),
116-
# (1649316166, 1501065279),
117-
# (1198642031, 458365203),
118-
# (442452829, 969558243),
119-
# (1200195957, 1876681249),
120-
# (1945678308, 962194431),
121-
# (949569752, 1077359051),
122-
# (2099272109, 265679591),
123-
# (587775847, 791886952),
124-
# (626863973, 1582116761),
125-
# (1003550677, 1676571504),
126-
# (1358625013, 1476289907),
127-
# (1008269081, 1117239683),
128-
# (2109153755, 1503178135),
129-
# (65212616, 1341148412),
130-
# (1851925803, 902714229),
131-
# (2137491580, 1331438416),
132-
# (1454235444, 58133212),
133-
# (675580731, 831516153),
134-
# (1754296375, 285337308),
135-
# (1821177336, 526856546),
136-
# (2130093701, 362935496),
137-
# (70062080, 750214563),
138-
# (1503113964, 210465667),
139-
# (1130186590, 1381224997),
140-
# (2005789796, 1846331200),
141-
# (1476653312, 1330597961),
142-
# (1174277203, 593162892),
143-
# (174182291, 1729496551),
144-
# (401846963, 792803163),
145-
# (973512717, 565661843),
146-
# (638171722, 863554642),
147-
# (2122881600, 53838754),
148-
# ]
149-
# add, mul = ret_nums[i]
150-
# return (add + mul * subtraction) % MBIG
112+
def invert_sample(rand, i):
113+
ret_nums = [
114+
(1796695496, 1821612595),
115+
(1891800662, 1409645351),
116+
(1610885040, 587487227),
117+
(1696684875, 1455237719),
118+
(2130791390, 767595827),
119+
(1929834895, 129867698),
120+
(2121031305, 1742476897),
121+
(883591791, 2106280948),
122+
(648963137, 1348178552),
123+
(795631511, 1633747307),
124+
(796601229, 1716765365),
125+
(816410995, 1096882805),
126+
(1918475170, 1559029571),
127+
(381881470, 1466324819),
128+
(641551938, 297465690),
129+
(316080314, 75192552),
130+
(270400817, 1823883914),
131+
(1585239219, 729323790),
132+
(2120325298, 2118823669),
133+
(230186630, 726040245),
134+
(547167837, 1241601023),
135+
(2138164393, 2086904121),
136+
(213491428, 315710936),
137+
(729980750, 468302249),
138+
(1512126699, 1259943578),
139+
(1739690517, 1712653821),
140+
(918222596, 653087170),
141+
(157253577, 2061948226),
142+
(970904747, 309988906),
143+
(1801285644, 1451072879),
144+
(584087765, 1485089530),
145+
(1256192415, 888247729),
146+
(1100808447, 2137905013),
147+
(1705889883, 643403504),
148+
]
149+
mul, add = ret_nums[i]
150+
return (add + mul * rand) % MBIG
151151

152152

153153
def sample_all(seed):
@@ -167,63 +167,12 @@ def test_sampel_seed():
167167
if my_r != r:
168168
raise Exception("sample_seed test failed on\n sample(%d)[%d] == %d should be %d" % (seed, ri, my_r, r))
169169

170-
test_sampel_seed()
171-
172-
ret_nums = [
173-
(1559595546, 1121899819),
174-
(1755192844, 630111683),
175-
(1649316166, 1501065279),
176-
(1198642031, 458365203),
177-
(442452829, 969558243),
178-
(1200195957, 1876681249),
179-
(1945678308, 962194431),
180-
(949569752, 1077359051),
181-
(2099272109, 265679591),
182-
(587775847, 791886952),
183-
(626863973, 1582116761),
184-
(1003550677, 1676571504),
185-
(1358625013, 1476289907),
186-
(1008269081, 1117239683),
187-
(2109153755, 1503178135),
188-
(65212616, 1341148412),
189-
(1851925803, 902714229),
190-
(2137491580, 1331438416),
191-
(1454235444, 58133212),
192-
(675580731, 831516153),
193-
(1754296375, 285337308),
194-
(1821177336, 526856546),
195-
(2130093701, 362935496),
196-
(70062080, 750214563),
197-
(1503113964, 210465667),
198-
(1130186590, 1381224997),
199-
(2005789796, 1846331200),
200-
(1476653312, 1330597961),
201-
(1174277203, 593162892),
202-
(174182291, 1729496551),
203-
(401846963, 792803163),
204-
(973512717, 565661843),
205-
(638171722, 863554642),
206-
(2122881600, 53838754),
207-
]
208-
invert_nums = []
209-
for add, mul in ret_nums:
210-
# giving rand = add + mul * seed mod MBIG
211-
# produce the inverted numer
212-
# ie:
213-
# rand - add = mul * seed
214-
# (rand - add) * mul^-1 = seed
215-
# rand * mul^-1 - add*mul^-1 = seed
216-
# where mul^-1 is inv_mul
217-
# -1 * add*mul^-1 is inv_add
218-
# inv_mul is found using fermats little therom: https://en.wikipedia.org/wiki/Fermat%27s_little_theorem
219-
# since MBIG is prime (Mersenne prime 2^31-1)
220-
inv_mul = pow(mul, MBIG - 2, MBIG)
221-
222-
# double check it's inverted
223-
assert((mul * inv_mul) % MBIG == 1)
224-
inv_add = (-1 * add * inv_mul) % MBIG
170+
for seed in range(4096 * 16):
171+
for i in range(34):
172+
r = sample_seed(seed, i)
173+
inv_r = invert_sample(r, i)
174+
if inv_r != seed:
175+
raise Exception("Inversion test failed on seed: %d rand: %d i: %d inv: %d" % (seed, r, i, inv_r))
225176

226-
invert_nums.append((inv_mul, inv_add))
177+
test_sampel_seed()
227178

228-
print(invert_nums)
229-
# [(1796695496, 1821612595), (1891800662, 1409645351), (1610885040, 587487227), (1696684875, 1455237719), (2130791390, 767595827), (1929834895, 129867698), (2121031305, 1742476897), (883591791, 2106280948), (648963137, 1348178552), (795631511, 1633747307), (796601229, 1716765365), (816410995, 1096882805), (1918475170, 1559029571), (381881470, 1466324819), (641551938, 297465690), (316080314, 75192552), (270400817, 1823883914), (1585239219, 729323790), (2120325298, 2118823669), (230186630, 726040245), (547167837, 1241601023), (2138164393, 2086904121), (213491428, 315710936), (729980750, 468302249), (1512126699, 1259943578), (1739690517, 1712653821), (918222596, 653087170), (157253577, 2061948226), (970904747, 309988906), (1801285644, 1451072879), (584087765, 1485089530), (1256192415, 888247729), (1100808447, 2137905013), (1705889883, 643403504)]

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /