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 6914723
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:23:05+00:00 2026-05-27T09:23:05+00:00

There is known Random(0,1) function, it is a uniformed random function, which means, it

  • 0

There is known Random(0,1) function, it is a uniformed random function, which means, it will give 0 or 1, with probability 50%. Implement Random(a, b) that only makes calls to Random(0,1)

What I though so far is, put the range a-b in a 0 based array, then I have index 0, 1, 2…b-a.

then call the RANDOM(0,1) b-a times, sum the results as generated idx. and return the element.

However since there is no answer in the book, I don’t know if this way is correct or the best. How to prove that the probability of returning each element is exactly same and is 1/(b-a+1) ?

And what is the right/better way to do this?

  • 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-05-27T09:23:05+00:00Added an answer on May 27, 2026 at 9:23 am

    If your RANDOM(0, 1) returns either 0 or 1, each with probability 0.5 then you can generate bits until you have enough to represent the number (b-a+1) in binary. This gives you a random number in a slightly too large range: you can test and repeat if it fails. Something like this (in Python).

    def rand_pow2(bit_count):
        """Return a random number with the given number of bits."""
        result = 0
        for i in xrange(bit_count):
            result = 2 * result + RANDOM(0, 1)
        return result
    
    def random_range(a, b):
        """Return a random integer in the closed interval [a, b]."""
        bit_count = math.ceil(math.log2(b - a + 1))
        while True:
            r = rand_pow2(bit_count)
            if a + r <= b:
                return a + r
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Are there any known design principles, best-practices and design patterns that one can follow
Is there any known hashing algorithm, which for similar input returns similar output? I
Is there a simple algorithm to encrypt integers? That is, a function E(i,k) that
There is a reference table that shows the execution cost of every php function?
Lets assume i have a function that takes 32bit integer in, and returns random
I have a legacy function that looks like this: int Random() const { return
Possible Duplicate: how to get uniformed random between a, b by a known uniformed
i just get one function from this site which describe that how to generate
Are there known issues with storing user defined types within index organized tables in
Are there any known issues with ASP.Net Ajax and IE6 in Windows 2000 machines

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.