View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
JE McGimpsey JE McGimpsey is offline
external usenet poster
 
Posts: 4,624
Default Randomize three variables subject to sum always equal to 1

One way:

Public Function ThreeRandsAddToOne() As Variant
Dim vTemp As Variant
Application.Volatile
ReDim vTemp(0 To 2)
vTemp(0) = Round(Rnd, 15)
vTemp(1) = Round(Rnd * (1 - vTemp(0)), 15)
vTemp(2) = 1 - (vTemp(0) + vTemp(1))
ThreeRandsAddToOne = Application.Transpose(vTemp)
End Function


Call by array-entering into a vertical range of three cells:

=ThreeRandsAddToOne()



In article ,
emsfeld wrote:

I need a code that randomly selects three real numbers between 0 and 1
and assigns these to three variables. I defined the variables as w(0),
w(1), w(2). The problem is that the sum has to equal 1! Always! So
that:

w(0) + w(1) + w(2) = 1