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

  • Home
  • SEARCH
  • 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 8062125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T10:33:47+00:00 2026-06-05T10:33:47+00:00

I have seen this question asked a lot but never seen a true concrete

  • 0

I have seen this question asked a lot but never seen a true concrete answer to it. So I am going to post one here which will hopefully help people understand why exactly there is “modulo bias” when using a random number generator, like rand() in C++.

  • 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-05T10:33:50+00:00Added an answer on June 5, 2026 at 10:33 am

    So rand() is a pseudo-random number generator which chooses a natural number between 0 and RAND_MAX, which is a constant defined in cstdlib (see this article for a general overview on rand()).

    Now what happens if you want to generate a random number between say 0 and 2? For the sake of explanation, let’s say RAND_MAX is 10 and I decide to generate a random number between 0 and 2 by calling rand()%3. However, rand()%3 does not produce the numbers between 0 and 2 with equal probability!

    When rand() returns 0, 3, 6, or 9, rand()%3 == 0. Therefore, P(0) = 4/11

    When rand() returns 1, 4, 7, or 10, rand()%3 == 1. Therefore, P(1) = 4/11

    When rand() returns 2, 5, or 8, rand()%3 == 2. Therefore, P(2) = 3/11

    This does not generate the numbers between 0 and 2 with equal probability. Of course for small ranges this might not be the biggest issue but for a larger range this could skew the distribution, biasing the smaller numbers.

    So when does rand()%n return a range of numbers from 0 to n-1 with equal probability? When RAND_MAX%n == n - 1. In this case, along with our earlier assumption rand() does return a number between 0 and RAND_MAX with equal probability, the modulo classes of n would also be equally distributed.

    So how do we solve this problem? A crude way is to keep generating random numbers until you get a number in your desired range:

    int x; 
    do {
        x = rand();
    } while (x >= n);
    

    but that’s inefficient for low values of n, since you only have a n/RAND_MAX chance of getting a value in your range, and so you’ll need to perform RAND_MAX/n calls to rand() on average.

    A more efficient formula approach would be to take some large range with a length divisible by n, like RAND_MAX - RAND_MAX % n, keep generating random numbers until you get one that lies in the range, and then take the modulus:

    int x;
    
    do {
        x = rand();
    } while (x >= (RAND_MAX - RAND_MAX % n));
    
    x %= n;
    

    For small values of n, this will rarely require more than one call to rand().


    Works cited and further reading:

    • CPlusPlus Reference

    • Eternally Confuzzled


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

Sidebar

Related Questions

This question seems to have been asked a lot, but I haven't seen an
I have seen this question asked here a few times, but most question didn't
I've seen this question asked here but I haven't found one that is answered
I have seen this question asked a few times but I have not seen
I have seen this question asked many times but none of the answers seem
I have seen this question being asked couple of times on this site but
I have seen this question asked previously but can not find a clear explanation
I've seen this question asked all over the net, but haven't seen an answer
I have seen this question asked several times but the answers have so far
While I have seen this question asked, I haven't seen the answer. I just

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.