0
$\begingroup$

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?

Thanks in advance

$\endgroup$
  • 2
    $\begingroup$ So you actually need only 1 random number ? $\endgroup$ – b.gates.you.know.what Oct 25 '14 at 16:16
  • $\begingroup$ I need that the sum of my ramdom number equal to 2 in my example I get 1.84 $\endgroup$ – Garuda Ikëtum Oct 25 '14 at 16:18
  • $\begingroup$ 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. $\endgroup$ – rhermans Oct 25 '14 at 16:19
  • $\begingroup$ yes OK @rhermans $\endgroup$ – Garuda Ikëtum Oct 25 '14 at 16:21
  • 2
    $\begingroup$ n Normalize[RandomReal[1, m],Total] $\endgroup$ – Kuba Oct 25 '14 at 17:05
1
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ Thanks very much it is exactly that. $\endgroup$ – Garuda Ikëtum Oct 25 '14 at 17:21
  • $\begingroup$ but there is a problem when the sum is big. the random numbers are not among 0 and 1 $\endgroup$ – Garuda Ikëtum Oct 25 '14 at 18:02
  • $\begingroup$ @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. $\endgroup$ – Bob Hanlon Oct 25 '14 at 18:47
  • $\begingroup$ So it's impossible to have ramdom numbers among 0 and 1 when I try list=random[3, 3] $\endgroup$ – Garuda Ikëtum Oct 25 '14 at 18:57
  • $\begingroup$ @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. $\endgroup$ – Bob Hanlon Oct 26 '14 at 15:55
0
$\begingroup$

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}
$\endgroup$
  • $\begingroup$ Thanks @rhermans. But I need a list of number not a pair like this {0.5, 0.5, 0.5, 0.5} $\endgroup$ – Garuda Ikëtum Oct 25 '14 at 16:29
  • $\begingroup$ 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. $\endgroup$ – rhermans Oct 25 '14 at 16:32
  • $\begingroup$ Ok @rhermans I edit my question and hope it is OK now $\endgroup$ – Garuda Ikëtum Oct 25 '14 at 16:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.