N.B. Thanks to @arcadeprecinct @arcadeprecinct and @raystafarian @raystafarian for suggestions and improvements.
N.B. Thanks to @arcadeprecinct and @raystafarian for suggestions and improvements.
N.B. Thanks to @arcadeprecinct and @raystafarian for suggestions and improvements.
Mathematical Note:
Allowing
Allowing for any value space (say, numbers from 2.7 to 3.81 with values of 3 decimal places):
Mathematical Note:
Allowing for any value space (say, numbers from 2.7 to 3.81 with values of 3 decimal places):
Mathematical Note:
Allowing for any value space (say, numbers from 2.7 to 3.81 with values of 3 decimal places):
Public Function RandList(ByVal minValue As Double, ByVal maxValue As Double, ByVal targetSum As Double, Optional ByVal numDecimalPlaces As Long = 0) As Double()
sumTotal = WorksheetFunction.Floor(sumTotal, numDecimalPlaces) '/ In Case, e.g. the total is 100.24 but we're only working in whole numbers
Dim sumList() As LongDouble
ReDim sumList(1 To 1)
Dim remainder As Double
remainder = targetSum
Dim valueSpace As Double
valueSpace = maxValue - minValue + 1 * 10 ^ (-numDecimalPlaces) '/ 1 * 10 ^ (-numDecimalPlaces) is our Floor() buffer so we can still hit the top of the value space.
Dim i As Long
Dim rndNum As Double
Do Until remainder <= 0
i = i + 1
ReDim Preserve sumList(1 To i)
rndNum = WorksheetFunction.Floor(minValue + (Rnd() * valueSpace), numDecimalPlaces)
sumList(i) = rndNum
remainder = remainder - rndNum
Loop
sumList(i) = sumList(i) + remainder
RandList = sumList
End Function
Public Sub DescriptiveName()
Dim sumList() As LongDouble
sumList = RandList(0, 9, 100)
'/ Stuff to print sumList goes here
End Sub
N.B. Thanks to @arcadeprecinct and @raystafarian for pointing out various simplificationssuggestions and improvements.
Public Function RandList(ByVal minValue As Double, ByVal maxValue As Double, ByVal targetSum As Double, Optional ByVal numDecimalPlaces As Long = 0)
sumTotal = WorksheetFunction.Floor(sumTotal, numDecimalPlaces) '/ In Case, e.g. the total is 100.24 but we're only working in whole numbers
Dim sumList() As Long
ReDim sumList(1 To 1)
Dim remainder As Double
remainder = targetSum
Dim valueSpace As Double
valueSpace = maxValue - minValue + 1 * 10 ^ (-numDecimalPlaces) '/ 1 * 10 ^ (-numDecimalPlaces) is our Floor() buffer so we can still hit the top of the value space.
Dim i As Long
Dim rndNum As Double
Do Until remainder <= 0
i = i + 1
ReDim Preserve sumList(1 To i)
rndNum = WorksheetFunction.Floor(minValue + (Rnd() * valueSpace), numDecimalPlaces)
sumList(i) = rndNum
remainder = remainder - rndNum
Loop
sumList(i) = sumList(i) + remainder
RandList = sumList
End Function
Public Sub DescriptiveName()
Dim sumList() As Long
sumList = RandList(0, 9, 100)
'/ Stuff to print sumList goes here
End Sub
N.B. Thanks to @arcadeprecinct for pointing out various simplifications.
Public Function RandList(ByVal minValue As Double, ByVal maxValue As Double, ByVal targetSum As Double, Optional ByVal numDecimalPlaces As Long = 0) As Double()
sumTotal = WorksheetFunction.Floor(sumTotal, numDecimalPlaces) '/ In Case, e.g. the total is 100.24 but we're only working in whole numbers
Dim sumList() As Double
ReDim sumList(1 To 1)
Dim remainder As Double
remainder = targetSum
Dim valueSpace As Double
valueSpace = maxValue - minValue + 1 * 10 ^ (-numDecimalPlaces) '/ 1 * 10 ^ (-numDecimalPlaces) is our Floor() buffer so we can still hit the top of the value space.
Dim i As Long
Dim rndNum As Double
Do Until remainder <= 0
i = i + 1
ReDim Preserve sumList(1 To i)
rndNum = WorksheetFunction.Floor(minValue + (Rnd() * valueSpace), numDecimalPlaces)
sumList(i) = rndNum
remainder = remainder - rndNum
Loop
sumList(i) = sumList(i) + remainder
RandList = sumList
End Function
Public Sub DescriptiveName()
Dim sumList() As Double
sumList = RandList(0, 9, 100)
'/ Stuff to print sumList goes here
End Sub
N.B. Thanks to @arcadeprecinct and @raystafarian for suggestions and improvements.