mercredi 6 mai 2020

XOR Float for Sobol sequences

I am trying to construct quasi-random generator using the Sobol sequence framework. The last step requires to do XOR between floats, as the following :

XOR between floats : example

I have tried using the code in the first answer here :XOR between floats in python but encountered problems and had to tweak it (I had several errors, maybe because I run Python 3). So the new code is this :

from struct import pack, unpack
def xor_float(f1, f2):
   f1 = int(''.join(hex(e)[2:] for e in struct.pack('d',f1)),16)
   f2 = int(''.join(hex(e)[2:] for e in struct.pack('d',f2)),16)
   xor = f1 ^ f2
   xor = "{:016x}".format(xor)
   xor = ''.join(chr(int(xor[i:i+2],16)) for i in range(0,len(xor),2))

return struct.unpack('d',xor.encode('utf-8'))[0]

which yields the wrong result. I have trouble understanding why as I am not so familiar with bytes manipulation in Python.

Also, I do not really understand what the XOR for floats does exactly so it is hard for me to come up with an idea of my own. Can somebody help me fix the code, or understand the logic behind this last step of Sobol sequence generation?

Thank you,

Best




Aucun commentaire:

Enregistrer un commentaire