We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
But have not been able to implement a working solution so far.
Any pointers on where to look next, or how to break down the problem welcome. Thank you!
Below is my code in Python 2.
Right now I am attempting to assert that the first 10 values are correct (same as what is given in the first example). I'll use that as a way to debug further, unless there are other suggestions.
importtimeclassRandom:def__init__(self,seed=None):ifseedisNone:seed=int(time.time()*1000)self._seed=seeddefget_seed(self):returnself._seeddefset_seed(self,seed):self._seed=(seed^0x5deece66d)&((1<<48)-1)defnext(self,bits):ifbits<1:bits=1elifbits>32:bits=32self._seed=(self._seed*0x5deece66d+0xb)&((1<<48)-1)retval=self._seed>>(48-bits)ifretval&(1<<31):retval-=(1<<32)returnretvaldefnextInt(self,n=None):"""Returnrandomintin[0,`n`).If`n`isnotsupplied,random32-bitintwillbereturned"""ifnisNone:returnself.next(32)ifn<=0:raisevalueError("Argument must be positive!")ifnot(n&(n-1)):return(n*self.next(31))>>31bits=self.next(31)val=bits%nwhile(bits-val+n-1)<0:bits=self.next(31)val=bits%nreturnvaldeffind_seed(arr):v1=arr[0]v2=arr[1]seed=Noneforiinxrange(65536):seed=v1*65536+1if(((seed*0x5deece66d+0xB)&((1<<48)-1))>>16)==v2:breakreturnseeddefget_next_10(r):ret_arr=[]foriinxrange(10):ret_arr.append(r.nextInt(1000))returnret_arrn=int(raw_input().strip())test_case_1=[643,953,522,277,464,366,321,409,227,702]test_case_2=[877,654,2,715,229,255,712,267,19,832]foriinxrange(n):# get input arr=map(int,raw_input().strip().split(' '))# find seeds=find_seed(arr)# set seedr=Random()r.set_seed(s)# assert the first 10 numbers are correct from the simple use caseforjinxrange(10):print'test_case_1nextIntis:'+str(test_case_1[j])print'nextIntis:'+str(r.nextInt(1000))# get next tenr_a=get_next_10(r)print' '.join(str(x)forxinr_a)
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
PRNG Sequence Guessing
You are viewing a single comment's thread. Return to all comments →
Tough challenge! I've studied a lot of related information: https://en.wikipedia.org/wiki/Linear_congruential_generator https://jazzy.id.au/2010/09/20/cracking_random_number_generators_part_1.html https://jazzy.id.au/2010/09/21/cracking_random_number_generators_part_2.html https://www.youtube.com/playlist?list=PL5KkMZvBpo5CdoOxa3dqll2n6KsXqerYO http://crypto.stackexchange.com/questions/2086/predicting-values-from-a-linear-congruential-generator https://en.wikipedia.org/wiki/Chinese_remainder_theorem https://en.wikipedia.org/wiki/B%C3%A9zout%27s_identity http://codegolf.stackexchange.com/questions/69582/b%C3%A9zouts-identity
But have not been able to implement a working solution so far.
Any pointers on where to look next, or how to break down the problem welcome. Thank you!
Below is my code in Python 2.
Right now I am attempting to assert that the first 10 values are correct (same as what is given in the first example). I'll use that as a way to debug further, unless there are other suggestions.