I don't know what you mean snb. I did +16k tests. Randomize can be added but some will argue about that too.
For the routine in #6, it still needs some work to skip 0 entries.
Here is another routine. It too can have multiple duplicate numbers and 0(s).
Sub Test_geRandom() Dim a, i As Integer Application.ScreenUpdating = False Application.Calculation = xlCalculationManual Application.EnableEvents = False ActiveSheet.UsedRange.ClearContents For i = 1 To 5 'Columns.Count a = kRandom(100, 20, False) With Range(Cells(1, i), Cells(20, i)) .Value = WorksheetFunction.Transpose(a) End With Next i Application.ScreenUpdating = True Application.Calculation = xlCalculationAutomatic Application.EnableEvents = True End Sub 'Similar to, Gary Evans, https://stackoverflow.com/questions/37835877/generate-n-random-numbers-summed-upto-100-vba Function geRandom(aSum As Long, xItems As Long) Dim AryNumbers() As Long Dim LngCounter As Long 'ReDim AryNumbers(0) ReDim AryNumbers(0 To xItems - 1) Randomize Do Until LngCounter = aSum AryNumbers(UBound(AryNumbers, 1)) = Int(xItems * Rnd + 1) If (LngCounter + AryNumbers(UBound(AryNumbers, 1))) > aSum Then AryNumbers(UBound(AryNumbers, 1)) = aSum - LngCounter Else LngCounter = LngCounter + AryNumbers(UBound(AryNumbers, 1)) 'ReDim Preserve AryNumbers(UBound(AryNumbers, 1) + 1) End If Loop geRandom = AryNumbers End Function




Reply With Quote