Я нашел код, который создает случайные числа по целевой сумме.
Function RandTot(iTot As Long, iLo As Long, iHi As Long, _
Optional bVol As Boolean = False) As Variant
Dim nNum As Long
Dim i As Long
Dim ad() As Double
Dim iTry As Long
If bVol Then Application.Volatile
With Application.Caller
If .Rows.Count > 1 And .Columns.Count > 1 Then
RandTot = "Enter as row or column vector only!"
Exit Function
End If
nNum = .Count
End With
If iHi < iLo Or _
iTot < nNum * iLo Or _
iHi > iTot Then
RandTot = CVErr(xlErrValue)
Exit Function
End If
ReDim ad(1 To nNum)
Randomize
With WorksheetFunction
Do
iTry = iTry + 1
If iTry > 200 Then
RandTot = "Time-out"
Exit Function
End If
For i = 1 To nNum - 1
ad(i) = RandBetw(iLo, iHi)
Next i
ad(i) = iTot - .Sum(ad) + ad(i)
Loop Until .Min(ad) >= iLo And .Max(ad) <= iHi
End With
RandTot = ad
End Function
Function RandBetw(iLo, iHi) As Long
RandBetw = (Rnd * (iHi - iLo) + Rnd * (iHi - iLo) + Rnd * (iHi - iLo)) / 4 + iLo
End Function
Сначала я умножаю целевое среднее на N, затем использую этот код, но иногда даю время ожидания.