Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9047391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T11:53:52+00:00 2026-06-16T11:53:52+00:00

I am trying to implement my own version of the Random class from the

  • 0

I am trying to implement my own version of the Random class from the python standard library. I can generate random bits and I implemented the getrandbits(n) function. But the superclass does not use this function to calculate the returned float from random(). So I have to implement that by myself:

def random(self):
    exp = 0x3FF0000000000000
    mant = self.getrandbits(52)
    return struct.unpack("d", struct.pack("q", exp^mant))[0]-1.0

I am using a sign of 0 (positive), an exponent of 1023 (2^0 = 1) and a random mantisse. So I get a number from [1.0, 2.0). The random() function must return a number in [0.0, 1.0) so I subtract 1.0 before returning.
As I’m not an expert when it comes to floating point numbers I’m not sure this is done the right way. Don’t I lose precision by subtracting? Can I build the number from random bits so that it’s in [0.0, 1.0) without subtraction?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T11:53:53+00:00Added an answer on June 16, 2026 at 11:53 am

    Your implementation is fine: assuming that getrandbits itself is random enough, your implementation will produce each number of the form n / 2^52 for 0 <= n < 2^52 with equal probability, so it’s a good approximation to the uniform distribution on [0, 1). The subtraction you use is not a problem: the result from that subtraction is always exactly representable, so there’s no rounding or precision loss involved in the subtraction.

    Python’s random() implementation does something more along the lines of return self.getrandbits(53) / 2**53. The effects are similar, except that the distribution of outputs is now twice as fine: you get each number of the form n / 2^53 for 0 <= n < 2^53 with equal probability. It’s unlikely that most applications would notice the difference between these two implementations in practice. If you care about speed, it’s likely that this is faster, too, though as always you should profile to find out whether that’s actually the case.

    Neither of these is perfect: there are around 2^62 distinct IEEE 754 binary64 floats in the range [0.0, 1.0), and your implementation can only generate 2^52 distinct outputs, so most of those floats can never be generated by either of the above implementations. A better random() implementation could generate each floating-point number x in the range [0.0, 1.0] with a probability equal to the length of the subinterval of [0.0, 1.0) that rounds to x under some form of round-to-nearest. However, such an implementation would be significantly more complicated (though not particularly hard to implement), and few applications would benefit from the larger output set. As the Zen of Python says: “Practicality beats purity.”


    EDIT: To illustrate the last paragraph above, here’s some code. The uniform function uses getrandbits to generate uniformly distributed floats on [0, 1] according to the above description.

    """                                                                                                                                                                                                                                         
    High quality uniform random variable on [0, 1].                                                                                                                                                                                             
    
    Simulates round(X) where X is a real random variable uniformly distributed on                                                                                                                                                               
    the interval [0, 1] and round is the usual round-to-nearest rounding function                                                                                                                                                               
    from real numbers to floating-point.                                                                                                                                                                                                        
    
    """
    from __future__ import division
    import random
    
    precision = 53
    emin = -1021
    
    def random_significand():
        return (random.getrandbits(precision) + 1) // 2 / (2**precision)
    
    def uniform():
        for i in xrange(1 - emin):
            if random.getrandbits(1):
                return (random_significand() + 0.5) / 2**i
        # Subnormal                                                                                                                                                                                                                             
        return random_significand() / 2**i
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to implement my own version of vector class without using iterators. Here
I am trying to implement my own File class, which extends from the java.io.File
I'm trying to implement my own version of the 'cd' command that presents the
I'm trying to implement my own List system in Java. the List class file
I'm trying to implement my own autocomplemention system (result is pull from an sqlite
I'm trying to implement my own version of NSViewController (for backwards compatibility), and I've
As part of learning Haskell, I am trying to implement my own version of
Im trying to implement my own glOtho function from the opengles docs http://www.khronos.org/opengles/documentation/opengles1_0/html/glOrtho.html to
I'm trying to implement my own GenericIdentity implementation but keep receiving the following error
I am trying to implement my own authentication method for AuthKit and am trying

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.