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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:00:56+00:00 2026-06-15T04:00:56+00:00

Assuming I have a function that returns a random bit, is it possible to

  • 0

Assuming I have a function that returns a random bit, is it possible to write a function that uniformly generates a random number within a certain range and always terminates?

I know how to do this so that it should (and probably will) terminate. I was just wondering if it’s possible to write one that is guaranteed to terminate (and it doesn’t have to be particularly efficient. What complexity would it have?

Here is a code for the not always terminating version

int random(int n)
{
  while(true)
  {
    int r = 0;
    for (int i = 0; i < ceil(log(n)); i++)
    {
      r = r<<1;
      r = r|getRandomBit();
    }

    if(r<n)
    {
      return r;
    }
  }
}
  • 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-15T04:00:57+00:00Added an answer on June 15, 2026 at 4:00 am

    If the size of the range isn’t a power of 2, you can’t get an exactly uniform distribution except through what amounts to rejection sampling. You can get as close as you like to uniform, however, by sampling once from a large range, and dividing the smaller range into it.

    For instance, while you can’t uniformly sample between 1 and 10, you can quite easily sample between 1 and 1024 by picking 10 random bits, and figure out some way of equitably dividing that into 10 intervals of about the same size.

    Choosing additional bits has the effect of halving the largest error (from true uniformity) you have to see in your choices… so the error decreases exponentially as you choose more bits.

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

Sidebar

Related Questions

I have some issues with C++ implementation of a function that generates random integers
I have a prime_factorize function that returns a dictionary mapping from prime divisors to
I have a prime_factorize function that returns a dictionary mapping from prime divisors to
I have a random number generator (RNG) function. -(double) generateRandomNumber { ... do some
I am interested in a function rand(x, y, seed) that returns (pseudo) random numbers
Assuming I have a function like this my_method(const vector<const T*> & param); I wonder
Assuming I have a JavaScript function like this... function Object1() { this.var1; this.var2; this.var3;
Assuming I have the following two JQuery functions - The first, which works: $(#myLink_931).click(function
Assuming I have a ASP.NET MVC 3 application that runs in a web farm
Say that have the following CTE that returns the level of some tree data

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.