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???
 

JoeMo

MrExcel MVP
Joined
May 26, 2009
Messages
16,666
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?
 

shg

MrExcel MVP
Joined
May 7, 2008
Messages
21,714
Office Version
2010
Platform
Windows
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
 

Forum statistics

Threads
1,081,747
Messages
5,361,038
Members
400,610
Latest member
ebey

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top