Randomly dividing an integer (non-uniform distribution)

I am trying to write a code that randomly divides a number N into M parts (in other words, I want to generate M random numbers whose sum adds up to N). However, I want to allow the generated numbers to vary considerably in magnitude.

My current code generates numbers that are all in the same ballpark:

N = 1;
n = rand(1,M);
TOT = sum(n);
numbers = (n/TOT)*N

For instance, for

, it yields:

0.0716    0.0184    0.0498    0.0204    0.0748    0.0528    0.0120    0.0194    0.0650    0.0721    0.0459    0.0744    0.0785    0.0248
0.0414 0.0326 0.0786 0.0778 0.0608 0.0290

As one can see, the number

is so evenly distributed that all generated numbers have the same (or close) orders of magnitude.

Is there a simple way to tweak this code so that it can generate numbers that are widely different in magnitude (e.g. 0.7 and 0.01)?

Any suggestions would be greatly appreciated.

NOTE:-

Matlabsolutions.com provide latest MatLab Homework Help,MatLab Assignment Help for students, engineers and researchers in Multiple Branches like ECE, EEE, CSE, Mechanical, Civil with 100% output.Matlab Code for B.E, B.Tech,M.E,M.Tech, Ph.D. Scholars with 100% privacy guaranteed. Get MATLAB projects with source code for your learning and research.

There are any number of ways to do that, depending on exactly what you mean by “considerably”. Here are a few:

%% Transform the numbers so they aren't uniform to begin with.
M=20;
N = 1;
n = rand(1,M).^10; % Increase the exponent to increase the size variation. A lot of numbers will be near zero.
TOT = sum(n);
numbers = (n/TOT)*N
%% Generate progressively smaller numbers by generating each new one as a uniform(0,1) fraction of
% what is left of your total.
M=20;
n = rand(1,M);

SEE COMPLETE ANSWER CLICK THE LINK

--

--

--

Simple! That is me, a simple person. I am passionate about knowledge and reading. That’s why I have decided to write and share a bit of my life and thoughts to.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Creating Data Lake using AWS S3, Glue, and Athena

Adobe Analytics Account Usage Logs: A List of Surprises & Should-Knows

How To Progress From A Beginner To An Intermediate Level Software Developer

Building the clone of the Yatra Website.

tuple

Obfuscate your application dll files with Obfuscar

Create A Better Flutter App.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Technical Source

Technical Source

Simple! That is me, a simple person. I am passionate about knowledge and reading. That’s why I have decided to write and share a bit of my life and thoughts to.

More from Medium

How to draw 2 random number simultaneously which are smaller than 1 together?

Assay models: blue-collar AI in the biology lab

Control Structure in Python

Support Vector Machine with Kernels and Python Iterators