This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author mark.dickinson
Recipients belopolsky, mark.dickinson, orsenthil, pitrou, rhettinger, terry.reedy, vstinner
Date 2010-06-23.19:37:53
SpamBayes Score 1.68307e-06
Marked as misclassified No
Message-id <>
> * possibly providing a C version of rnd2()

If recoding in C is acceptable, I think there may be better ( = simpler and faster) ways than doing a direct translation of rnd2.

For example, for small k, the following algorithm for randrange(k) suffices:

 - take a single 32-bit deviate (generated using genrand_int32)
 - multiply by k (a 32-by-32 to 64-bit widening multiply) and return
   the high 32-bits of the result, provided that the bottom half
   of the product is <= 2**32 - k (almost always true, for small k).
 - consume extra random words as necessary in the case that the bottom
   half of the product is > 2**32 - k.

I can provide code (with that 3rd step fully expanded) if you're interested in this approach.

This is likely to be significantly faster than a direct translation of rnd32, since in the common case it requires only: one 32-bit deviate from MT, one integer multiplication, one subtraction, and one comparison.  By comparison, rnd2 uses (at least) two 32-bit deviates and massages them into a float, before doing arithmetic with that float.

Though it's possible (even probable) that any speed gain would be insignificant in comparison to the rest of the Python machinery involved in a single randrange call.
Date User Action Args
2010-06-23 19:37:55mark.dickinsonsetrecipients: + mark.dickinson, rhettinger, terry.reedy, belopolsky, orsenthil, pitrou, vstinner
2010-06-23 19:37:55mark.dickinsonsetmessageid: <>
2010-06-23 19:37:54mark.dickinsonlinkissue9025 messages
2010-06-23 19:37:53mark.dickinsoncreate