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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:09:07+00:00 2026-06-18T08:09:07+00:00

Why does this code generates uniformly distributed numbers? I have some difficulties in understanding

  • 0

Why does this code generates uniformly distributed numbers? I have some difficulties in understanding it. Could someone explain? Thanks.

int RandomUniform(int n) {  
  int top = ((((RAND_MAX - n) + 1) / n) * n - 1) + n;  
  int r;  
  do {  
    r = rand();  
  } while (r > top);  
  return (r % n);  
}

update: I do understand why rand()%n doesn’t give you a uniformly distributed sequence. My question is why the

top = ((((RAND_MAX - n) + 1) / n) * n - 1) + n;

What’s the concern here? I think a simple top = RAND_MAX / n * n would do.

  • 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-18T08:09:07+00:00Added an answer on June 18, 2026 at 8:09 am

    The function assumes that rand() is uniformly distributed; whether or not that is a valid assumption depends on the implementation of rand().

    Given a uniform rand(), we can get a random number in the range [0,n) by calculating rand()%n. However, in general, this won’t be quite uniform. For example, suppose n is 3 and RAND_MAX is 7:

    rand()      0 1 2 3 4 5 6 7
    rand() % n  0 1 2 0 1 2 0 1
    

    We can see that 0 and 1 come up with a probability of 3/8, while 2 only comes up with a probability of 2/8: the distribution is not uniform.

    Your code discards any value of rand() greater or equal to the largest multiple of n that it can generate. Now each value has an equal probability:

    rand()      0 1 2 3 4 5 6 7
    rand() % n  0 1 2 0 1 2 X X
    

    So 0,1 and 2 all come up with a probability of 1/3, as long as we are not so unlucky that the loop never terminates.

    Regarding your update:

    I think a simple top = RAND_MAX / n * n would do.

    If RAND_MAX were an exclusive bound (one more than the actual maximum), then that would be correct. Since it’s an inclusive bound, we need to add one to get the exclusive bound; and since the following logic compares with > against an inclusive bound, then subtract one again after the calculation:

    int top = ((RAND_MAX + 1) / n) * n - 1;
    

    However, if RAND_MAX were equal to INT_MAX, then the calculation would overflow; to avoid that, subtract n at the beginning of the calculation, and add it again at the end:

    int top = (((RAND_MAX - n) + 1) / n) * n - 1 + n;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code which generates me two images. What it does is generate
What does this error mean? Here is the line of code that generates it
I have this code snippets that generates a signature for POSTs. The detail of
I have this code that generates a group ID with javascript. It's shown here:
Why does the Entity Framework generate nested SQL queries? I have this code var
Does this code always evaluate to false? Both variables are two's complement signed ints.
Does this code cause a memory leak: int main(){ int * a = new
Does this code work across all standard compliant C++ compilers (it works with g++)?
How does this code work to Find the next highest power of 2 for
Why does this code throw an Invalid Operation Exception? private SqlCommand cmd; // initialized

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.