#The standard random number generator built in to java.util.Random. #The 48-bit state is is modified by a linear congruential iteration: # state<----state*multiplier + modulus mod 2**48 #The nextInt method updates the state and returns a 32-bit result: bits 47..16 #of the state. #In this implementation we return a list of 4 bytes as the generator output. import bytestuff state=1L #The key is an ASCII string which we convert to a long, then grab the low-order #48 bits def init_state(key): global state bytelist=bytestuff.ascii_to_bytes(key) state=bytestuff.bytes_to_long(bytelist)&(0xffffffffffffL) def update_state(): global state state = (state *0x5deece66dL + 0xb)&(0xffffffffffffL) #The rng outputs 4 bytes at a time. def next_bytes(): update_state() #output 32 is bits 47..16 of new state; #this is the usual output of nextInt in java.util.Random output32=(state>>16)&(0xffffffff) blist=[int((output32>>(8*j))&0xff) for j in range(3,-1,-1)] return blist #This gives k' bytes, where k' is the smallest multiple of 4 >= k def get_bytes(k): blocks=k/4 if k%4!=0: blocks+=1 blist=[] for j in range(blocks): blist+=next_bytes() return blist