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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T00:11:17+00:00 2026-06-07T00:11:17+00:00

How do I divide a large positive integer n into m parts uniformly randomly

  • 0

How do I divide a large positive integer n into m parts uniformly randomly.
Post-condition: Adding up all the m parts should give n.

Below is my attempt(in java like pseudocode), but I don’t think it will give me uniformly random distribution.
I am first finding the average part avg by dividing n/m. Then I am generating m-1 random numbers which are around avg in magnitude(by alternately generating random numbers between 0 & avg, and *avg & 2*avg*. Then I am subtracting the sum of these m-1 numbers from original number n and setting that as the m‘th part.

Assume that the function rand(x, y) returns a random number uniformly between x and y.

int[] divideUniformlyRandomly(int n, int m)
{
    int[] res = new int[m];
    int avg = n / m;
    int sum = 0;
    bool alternator = false;
    for(int i = 0; i < m - 1; i++)
    {
        if(alternator == false)
        {
            res[i] = rand(0, avg);
            alternator = true;
        }
        else
        {
            res[i] = rand(avg, 2*avg);
            alternator = false;
        }
        sum += res[i];
    }
    res[m-1] = n - sum;
    return res;
}
  • 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-07T00:11:18+00:00Added an answer on June 7, 2026 at 12:11 am

    public double[] divideUniformlyRandomly(double number, int part) {
        double uniformRandoms[] = new double[part];
        Random random = new Random();
    
        double mean = number / part;
        double sum = 0.0;
    
        for (int i=0; i<part / 2; i++) {
            uniformRandoms[i] = random.nextDouble() * mean;
    
            uniformRandoms[part - i - 1] = mean + random.nextDouble() * mean;
    
            sum += uniformRandoms[i] + uniformRandoms[part - i -1];
        }
        uniformRandoms[(int)Math.ceil(part/2)] = uniformRandoms[(int)Math.ceil(part/2)] + number - sum;
    
        return uniformRandoms;
    }
    

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

Sidebar

Related Questions

I have a large dataset that I need to divide randomly into 5 almost
I have a large set of strings. I want to divide the strings into
I want to divide the window into 2 parts. Each part I can draw
How should I divide source files into projects (within one solution) to be able
The Problem I want to divide a grid (2D array) into random shaped parts
I am wondering how divide large scale application into projects. Does I need to
All numbers that divide evenly into x. I put in 4 it returns: 4,
I'm using Processing to divide a large image into a series of smaller, rectangular
iPad application I have a large text file which I want to divide into
I made small program to divide large pictures and take part of them. When

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.