Bit-twiddling

Lars Marius Garshol larsga at ifi.uio.no
Thu Jul 29 03:07:32 EDT 1999


* Tim Peters
| 
| Provided that seed was a long to begin with, this code almost works
| as is.  You just need to append "L" to the literals (for 69069 that
| isn't necessary, it just avoids the repeated expense of runtime
| widening; for 0xffffffff it *is* necessary on a 32-bit machine, else
| 0xffffffff == -1 gets widened to -1L and then the masking doesn't
| accomplish anything (i & -1L == i for all long i)).

Aha! I knew that 0xffffffff didn't work, but I had no idea that the
trick was to define it as a long. Thanks!
 
| The elements of mt are then all longs, but each one's *value* fits
| in a 32-bit C unsigned long: numerically, it computes the same
| values as would the C code.

Indeed it does. Thanks again! 

Based on this advice I took a stab at the rest of the algorithm and
now it actually works! Wheee! So if anyone wants a string of random
numbers with a period of 2**199937-1 and equi-distributed in 623
dimensions, just let me know. :-)
 
| if-you're-really-after-random-numbers-see-ivan-frohne-ly y'rs  - tim

I guessed that you would recognize the algorithm. :)

I wasn't really after random numbers as much as I was after some easy
diversion. This turned out to not be that easy for someone with a bit
(hah!) less than the necessary maths and C background. :(

Anyway, I see that Frohne uses the code I use above, except that his
69069 is L. But as you say that makes no difference. He doesn't use
the same code for number generation, though.

If I need more diversion I'll try to turn the C code into an extension
module as my first-ever extension module. Should be educational, at
least.

--Lars M.




More information about the Python-list mailing list