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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:58:13+00:00 2026-05-26T05:58:13+00:00

Question: Suppose you have a random number generator randn() that returns a uniformly distributed

  • 0

Question: Suppose you have a random number generator randn() that returns a uniformly distributed random number between 0 and n-1. Given any number m, write a random number generator that returns a uniformly distributed random number between 0 and m-1.

My answer:

-(int)randm() {
    int k=1;
    while (k*n < m) {
        ++k;
    }
    int x = 0;
    for (int i=0; i<k; ++i) {
        x += randn();
    }
    if (x < m) {
        return x;
    } else {
        return randm();
    }
}

Is this correct?

  • 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-26T05:58:13+00:00Added an answer on May 26, 2026 at 5:58 am

    You’re close, but the problem with your answer is that there is more than one way to write a number as a sum of two other numbers.

    If m<n, then this works because the numbers 0,1,...,m-1 appear each with equal probability, and the algorithm terminates almost surely.

    This answer does not work in general because there is more than one way to write a number as a sum of two other numbers. For instance, there is only one way to get 0 but there are many many ways to get m/2, so the probabilities will not be equal.

    Example: n = 2 and m=3

    0 = 0+0
    1 = 1+0 or 0+1
    2 = 1+1
    

    so the probability distribution from your method is

    P(0)=1/4
    P(1)=1/2
    P(2)=1/4
    

    which is not uniform.


    To fix this, you can use unique factorization. Write m in base n, keeping track of the largest needed exponent, say e. Then, find the biggest multiple of m that is smaller than n^e, call it k. Finally, generate e numbers with randn(), take them as the base n expansion of some number x, if x < k*m, return x, otherwise try again.

    Assuming that m < n^2, then

    int randm() {
    
        // find largest power of n needed to write m in base n
        int e=0;
        while (m > n^e) {
            ++e;
        }
    
        // find largest multiple of m less than n^e
        int k=1;
        while (k*m < n^2) {
            ++k
        }
        --k; // we went one too far
    
        while (1) {
            // generate a random number in base n
            int x = 0;
            for (int i=0; i<e; ++i) {
                x = x*n + randn(); 
            }
            // if x isn't too large, return it x modulo m
            if (x < m*k) 
                return (x % m);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just read this interesting question about a random number generator that never generates
I have recently come across an interesting question on strings. Suppose you are given
I have this question: suppose that the size of char is one byte and
Is there a difference between generating multiple numbers using a single random number generator
Quick question... Suppose I have a C# struct that contains a single reference member.
Context: C# 3.0, .Net 3.5 Suppose I have a method that generates random numbers
Suppose that you have 10 stations that are sending packets at a given time
I recently came across a question somewhere: Suppose you have an array of 1001
I have question about normalization. Suppose I have an applications dealing with songs. First
This is somewhat of a follow-up question to this question . Suppose I have

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.