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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:05:52+00:00 2026-05-27T20:05:52+00:00

Possible Duplicate: how to get uniformed random between a, b by a known uniformed

  • 0

Possible Duplicate:
how to get uniformed random between a, b by a known uniformed random function RANDOM(0,1)

In the book of Introduction to algorithms, there is an excise:

Describe an implementation of the procedure Random(a, b) that only makes calls to Random(0,1). What is the expected running time of your procedure, as a function of a and b? The probability of the result of Random(a,b) should be pure uniformly distributed, as Random(0,1)

For the Random function, the results are integers between a and b, inclusively. For e.g., Random(0,1) generates either 0 or 1; Random(a, b) generates a, a+1, a+2, …, b

My solution is like this:

for i = 1 to b-a
    r = a + Random(0,1)
return r

the running time is T=b-a

Is this correct? Are the results of my solutions uniformly distributed?

Thanks

What if my new solution is like this:

r = a
for i = 1 to b - a //including b-a
    r += Random(0,1)
return r

If it is not correct, why r += Random(0,1) makes r not uniformly distributed?

  • 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-27T20:05:52+00:00Added an answer on May 27, 2026 at 8:05 pm

    Others have explained why your solution doesn’t work. Here’s the correct solution:

    1) Find the smallest number, p, such that 2^p > b-a.

    2) Perform the following algorithm:

    r=0
    for i = 1 to p
        r = 2*r + Random(0,1)
    

    3) If r is greater than b-a, go to step 2.

    4) Your result is r+a

    So let’s try Random(1,3).
    So b-a is 2.
    2^1 = 2, so p will have to be 2 so that 2^p is greater than 2.

    So we’ll loop two times. Let’s try all possible outputs:

    00 -> r=0, 0 is not > 2, so we output 0+1 or 1.
    01 -> r=1, 1 is not > 2, so we output 1+1 or 2.
    10 -> r=2, 2 is not > 2, so we output 2+1 or 3.
    11 -> r=3, 3 is > 2, so we repeat.
    

    So 1/4 of the time, we output 1. 1/4 of the time we output 2. 1/4 of the time we output 3. And 1/4 of the time we have to repeat the algorithm a second time. Looks good.

    Note that if you have to do this a lot, two optimizations are handy:

    1) If you use the same range a lot, have a class that computes p once so you don’t have to compute it each time.

    2) Many CPUs have fast ways to perform step 1 that aren’t exposed in high-level languages. For example, x86 CPUs have the BSR instruction.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Get the Files inside a directory Is there a function that can
Possible Duplicate: PHP get all arguments as array? Within a javascript function arguments always
Possible Duplicate: How to get the address of an overloaded member function? I have
Possible Duplicate: Get a function's arity Say I have: function a(x) {}; function b(x,y)
Possible Duplicate: (C#) Get index of current foreach iteration Good morning, Is there any
Possible Duplicate: Get query string values in JavaScript I am writing a jQuery function
Possible Duplicate: Get content between two strings PHP Ok, here is my delima: here
Possible Duplicate: Get a MIME from an extention? is there any method in asp.net
Possible Duplicate: Get CVS history for a particular user Is it possible to list
Possible Duplicate: Get all files from VSS for a given date? I need to

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.