# A constraint to RandomReal for sum of numbers equal to arbitrary n

I want to generate some random numbers but respect a constraint.

So I use

RandomReal[{0,1},N,WorkingPrecision -> 2]


I want to fix a constraint in order that the sum of random numbers of my output equal to arbitrary N.

Have you some Idea?

• So you actually need only 1 random number ? Oct 25, 2014 at 16:16
• I need that the sum of my ramdom number equal to 2 in my example I get 1.84 Oct 25, 2014 at 16:18
• Hi, welcome to Mathematica.SE, please consider taking the tour so you learn the basics of the site. Once you gain enough reputation by making good questions you will be able to vote up and down both questions and answers. When you see good ones, please vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. As you receive help, try to give it too, by answering questions in your area of expertise. Oct 25, 2014 at 16:19
• yes OK @rhermans Oct 25, 2014 at 16:21
• n Normalize[RandomReal[1, m],Total]
– Kuba
Oct 25, 2014 at 17:05

EDIT: added capability for including negative numbers as part of a positive sum or positive numbers as part of a negative sum.

random[sum_, nbrElements_, init_: 0] :=
Module[{list = RandomReal[{init, 1}, nbrElements]},
sum*list/Total[list]];

list1 = random[2, 5]


{0.0325747, 0.251624, 0.67988, 0.876599, 0.159322}

Total[list1]


2.

list2 = random[500, 5]


{144.326, 111.266, 23.6429, 174.103, 46.6624}

Total[list2]


500.

Putting a negative value for the third argument will allow negative values even for a positive sum or positive numbers as part of a negative sum. Use of -1 will make positive and negative values equally likely.

list3 = random[20, 7, -1]


{5.42643, 1.9796, 7.17927, -1.59159, 6.64314, 1.3478, -0.98463}

Total[list3]


20.

• Thanks very much it is exactly that. Oct 25, 2014 at 17:21
• but there is a problem when the sum is big. the random numbers are not among 0 and 1 Oct 25, 2014 at 18:02
• @Garuda - it automatically scales to the required sum. Try list2 = random[500, 5]. I just added the ability to include negative values even for a positive sum. Oct 25, 2014 at 18:47
• So it's impossible to have ramdom numbers among 0 and 1 when I try list=random[3, 3] Oct 25, 2014 at 18:57
• @Garuda - I do not understand your comment. It is statistically impossible for three random real numbers on the interval [0, 1] to sum to three, i.e., they would all have to be identically one and the probability of any one of them being one is zero. If you intend for the random numbers to be random integers then you need to ask a new question and clarify what you want. Oct 26, 2014 at 15:55

As pointed out by @b.gatessucks, that constraint means that you only need a single random number.

If you need a single pair

{2 - #, #} & @ RandomReal[{0.5, 1.5}, WorkingPrecision -> 2]


of for many

{2 - #, #} & /@ RandomReal[{0.5, 1.5}, 10, WorkingPrecision -> 2]

{{0.6, 1.4}, {1.45, 0.55}, {0.6, 1.4}, {1.0, 1.0}, {1.05, 0.95}, {1.07, 0.93}, {1.36, 0.64}, {0.9, 1.1}, {0.7, 1.3}, {0.9, 1.1}}

Total /@ %

{2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0, 2.0}

• Thanks @rhermans. But I need a list of number not a pair like this {0.5, 0.5, 0.5, 0.5} Oct 25, 2014 at 16:29
• OK, you said before that you needed the sum of two numbers to be equal to 2. if you need the sum of an arbitrary number n then please edit your question and we will try to do our best. Oct 25, 2014 at 16:32
• Ok @rhermans I edit my question and hope it is OK now Oct 25, 2014 at 16:44