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:

For instance, for

, it yields:

    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.


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:

%%  Generate progressively smaller numbers by generating each new one as a uniform(0,1) fraction of
% what is left of your total.
n = rand(1,M);




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

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.