Generating Random numbers

MEscobar

New Member
Joined
May 9, 2014
Messages
1
Hi all,

I need help with generating random numbers that equal a specific total. for example:
I want random numbers from A1 to A15 that equal to 6
I would need the same for columns B through J

And as an added bonus stump question :) I need each of those rows to equal a specific number all the way accross.

So, Random numbers A1-A15 with the total of 6 in A16 (repeated for columns B-J)
Then, I need A1-J1 to total 8 in H1

Is this possible???
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Hi all,

I need help with generating random numbers that equal a specific total. for example:
I want random numbers from A1 to A15 that equal to 6
I would need the same for columns B through J

And as an added bonus stump question :) I need each of those rows to equal a specific number all the way accross.

So, Random numbers A1-A15 with the total of 6 in A16 (repeated for columns B-J)
Then, I need A1-J1 to total 8 in H1

Is this possible???

H1 is in the range A1:J1 - seems you are headed for a circular reference, is that what you want?
 
Upvote 0
UDF?

A​
B​
C​
D​
E​
F​
G​
H​
I​
J​
K​
L​
M​
1​
0.3210​
0.1205​
0.6963​
1.0563​
2.2265​
1.0927​
0.1454​
2.1877​
0.1491​
0.0045​
8.0000​
A1:A15: {=randlen(6, , 4)}
2​
0.0164​
B1:J1: {=randlen(8 - A1, ,4)}
3​
1.4515​
K1: =SUM(A1:J1)
4​
0.3952​
A16: =SUM(A1:A15)
5​
0.0571​
6​
0.5710​
7​
0.3370​
8​
0.4055​
9​
0.1814​
10​
0.1508​
11​
0.7157​
12​
0.6260​
13​
0.4687​
14​
0.0744​
15​
0.2283​
16​
6.0000​

Code:
Function RandLen(dTot As Double, _
                 Optional dMin As Double = 0#, _
                 Optional ByVal iSig As Long = 0, _
                 Optional bVolatile As Boolean = False) As Double()
    ' shg 2011, 2013

    ' UDF wrapper for adRandLen

    Dim adTmp()     As Double
    Dim adOut()     As Double
    Dim iRow        As Long
    Dim nRow        As Long
    Dim iCol        As Long
    Dim nCol        As Long

    If bVolatile Then Application.Volatile

    nRow = Application.Caller.Rows.Count
    nCol = Application.Caller.Columns.Count

    adTmp = adRandLen(dTot, nRow * nCol, dMin, iSig)
    ReDim adOut(1 To nRow, 1 To nCol)

    For iRow = 1 To nRow
        For iCol = 1 To nCol
            adOut(iRow, iCol) = adTmp((iRow - 1) * nCol + iCol)
        Next iCol
    Next iRow

    RandLen = adOut
End Function

Function adRandLen(ByVal dTot As Double, _
                   nOut As Long, _
                   Optional ByVal dMin As Double = 0#, _
                   Optional ByVal iSig As Long = 307) As Double()
    ' shg 2011

    ' Applies string-cutting to return an array of nOut
    ' numbers totalling dTot, with each in the range
    '    dMin <= number <= Round(dTot, iSig) - nOut * round(dMin, iSig)

    ' Each number is rounded to iSig decimals

    Dim iOut        As Long     ' index to iOut
    Dim jOut        As Long     ' sort insertion point
    Dim dRnd        As Double   ' random number
    Dim dSig        As Double   ' decimal significance (e.g., 1, 0.01, ...)
    Dim adOut()     As Double   ' output array

    dTot = WorksheetFunction.Round(dTot, iSig)
    dMin = WorksheetFunction.Round(dMin, iSig)
    If nOut < 1 Or dTot < nOut * dMin Then Exit Function

    ReDim adOut(1 To nOut)
    dSig = 10# ^ -iSig

    With New Collection
        .Add Item:=0#
        .Add Item:=dTot - nOut * dMin

        ' create the cuts
        For iOut = 1 To nOut - 1
            dRnd = Int(Rnd() * ((dTot - nOut * dMin) / dSig)) * dSig

            ' insertion-sort the cut
            For jOut = .Count To 1 Step -1
                If .Item(jOut) <= dRnd Then
                    .Add Item:=dRnd, After:=jOut
                    Exit For
                End If
            Next jOut
        Next iOut

        ' measure the lengths
        For iOut = 1 To nOut
            adOut(iOut) = .Item(iOut + 1) - .Item(iOut) + dMin
        Next iOut
    End With

    adRandLen = adOut
End Function
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,550
Members
449,088
Latest member
davidcom

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top