I’ve been trying to port this random number generator into CUDA. It’s a linear feedback shift register RNG, which satisfies the park-miller minimum standard. The source code can be found here: http://www.physics.udel.edu/~bnikolic/teaching/phys660/F90/rlfsr113.f90
After seeding, the random numbers are generated like so:
b = ishft(ieor(ishft(z1,6),z1),-13)
z1 = ieor(ishft(iand(z1,-2),18),b)
b = ishft(ieor(ishft(z2,2),z2),-27)
z2 = ieor(ishft(iand(z2,-8),2),b)
b = ishft(ieor(ishft(z3,13),z3),-21)
z3 = ieor(ishft(iand(z3,-16),7),b)
b = ishft(ieor(ishft(z4,3),z4),-12)
z4 = ieor(ishft(iand(z4,-128),13),b)
rand_num=ishft( ieor(ieor(ieor(z1,z2),z3),z4) , -1)*4.656612873077d-10
When this works correctly, I get a uniform distribution of random reals between 0 and 1. The problem is, when I port this random number generator into CUDA, I don’t get the same numbers, and I get negative numbers as well. I figured out that the problem was to do with the fact that in FORTRAN, when you do a negative bit shift to a negative 32-bit integer, it cycles back to the highest positive integer (e.g, ISHFT(-4,-1) = 2147483646
). But when I do the same on CUDA, the number stays negative (e.g, ISHFT(-4,-1) = -2).
Is there a way to set it so that CUDA gets the same results as the CPU? Is it something to do with unsigned integers perhaps?