1

Does anybody know how to generate two random numbers which sum is less than one?

I found only topics describing how to generate 2 random numbers which add up to 1 (trough normalization)

Ahmet Kakıcı
  • 6,294
  • 4
  • 37
  • 49
  • The obvious solution would be to normalize them to anything less than one. Do you want to achieve a certain distribution or do you want your numbers to have certain properties? – Dennis Jaheruddin Jan 23 '14 at 11:04
  • I see now that the condition is "sum _less than_", not "sum _equal to_". So I'm retracting my duplicate-question vote – Luis Mendo Jan 23 '14 at 11:45

3 Answers3

2
  1. Generate the first random number, r1.
  2. Generate a random number less than 1 to be the random sum.
  3. Define r2 as (sum - r1).
normKrumpe
  • 63
  • 5
  • I suppose you would need to generate sum first, and then make sure r1 is generated less than sum. (Or generate sum to be larger than r1) otherwise you could have `r1=0.8, sum=0.4, r3=-0.4` which I expect to be undesirable. – Dennis Jaheruddin Jan 23 '14 at 11:07
  • I wondered that too, but original post put no bounds on the two numbers...only on their sum. – normKrumpe Jan 23 '14 at 11:09
  • Your answer is indeed correct, but in this case I would recommend you to mention the assumption that negative numbers are allowed. That may help the asker even more! – Dennis Jaheruddin Jan 23 '14 at 11:14
  • Sorry if I bother you again... How I can do if I want to generate a third variable which squared value is less than the product of the first two variables? In practice what I'm trying to do is to generate a positive-definite 2x2 matrix... – Alessandro Beretta Jan 23 '14 at 12:00
1

Assuming you don't care too much about the distribution:

x = rand(2,1);
if sum(x)>1
   x=x/2;
end
Dennis Jaheruddin
  • 21,208
  • 8
  • 66
  • 122
0

Modifying normKrumpe's answer, I'd suggest

  1. Generate the sum as a random number.
  2. Generate a random number r1 less than (or <=?) the sum.
  3. r2 = sum-r1.
Community
  • 1
  • 1
glglgl
  • 89,107
  • 13
  • 149
  • 217