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

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
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?

[Table="width:, class:grid"][tr][td] [/td][td]
A​
[/td][td]
B​
[/td][td]
C​
[/td][td]
D​
[/td][td]
E​
[/td][td]
F​
[/td][td]
G​
[/td][td]
H​
[/td][td]
I​
[/td][td]
J​
[/td][td]
K​
[/td][td]
L​
[/td][td]
M​
[/td][/tr]
[tr][td]
1​
[/td][td]
0.3210​
[/td][td]
0.1205​
[/td][td]
0.6963​
[/td][td]
1.0563​
[/td][td]
2.2265​
[/td][td]
1.0927​
[/td][td]
0.1454​
[/td][td]
2.1877​
[/td][td]
0.1491​
[/td][td]
0.0045​
[/td][td]
8.0000​
[/td][td][/td][td]A1:A15: {=randlen(6, , 4)}[/td][/tr]

[tr][td]
2​
[/td][td]
0.0164​
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td]B1:J1: {=randlen(8 - A1, ,4)}[/td][/tr]

[tr][td]
3​
[/td][td]
1.4515​
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td]K1: =SUM(A1:J1)[/td][/tr]

[tr][td]
4​
[/td][td]
0.3952​
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td]A16: =SUM(A1:A15)[/td][/tr]

[tr][td]
5​
[/td][td]
0.0571​
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr][td]
6​
[/td][td]
0.5710​
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr][td]
7​
[/td][td]
0.3370​
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr][td]
8​
[/td][td]
0.4055​
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr][td]
9​
[/td][td]
0.1814​
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr][td]
10​
[/td][td]
0.1508​
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr][td]
11​
[/td][td]
0.7157​
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr][td]
12​
[/td][td]
0.6260​
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr][td]
13​
[/td][td]
0.4687​
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr][td]
14​
[/td][td]
0.0744​
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr][td]
15​
[/td][td]
0.2283​
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr][td]
16​
[/td][td]
6.0000​
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]
[/table]


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,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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