About Random number generator function

Ashwini Lambture

New Member
Joined
Apr 1, 2014
Messages
23
Hi All, I have Columns as

A B C D E
1 Age(18-24) Age(25-34) Age(35-44) Age(45-54) Age(55-Above)
2

I want random number generated in all the columnin 2nd row i.e. A,B,C,D,E such that whose sum(A+B+C+D+E) will be 100 thanks in advance.
 
Last edited:
And what is the code for Make1D ? some
<code>application.transpose(application.transpose(avInp... ?</code></pre>
 
Upvote 0

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Hm, third post in a row. Hope moderators are tolerant on this forum :)

Returning to simple formulas from post #2 on first page and to exactly follow Ashwini's last request. In O1:
Code:
=IF(COUNTIF($J1:J1,MAX($J1:$N1))=1,K1,J1)
copy to P1. In Q1 just:
Code:
=MAX($J1:$N1)
in R1 (and copy to S1):
Code:
=IF(COUNTIF(M1:$N1,MAX($J1:$N1))=1,L1,M1)
copy all J1:S1 down as many rows as needed

in A2 minor change and also copy right and down:
Code:
=[COLOR=#ff0000]O[/COLOR]1*100/SUM($[COLOR=#ff0000]O[/COLOR]1:$[COLOR=#ff0000]S[/COLOR]1)

But ... I really liked the code in previous post and distribution it generates - use FREQUENCY array formula to have a look on them.

Hi Kaper, I m not getting how to do it ,.....I have tried with ur formulas but but I m not getting the result what I wanted so can u plz try it in a excel worbook and attach it with your reply .............Thank you very much for ur contribution .Take my columns of ages ok....
 
Upvote 0
Here is a practical approach to get the data you need – I do not know much about random properties, so I can not test how much this approach is „random”.

In a new workbook put in A1:

=Randbetween(0,100) then copy right to E1.

Click on A1 and enter into the Name box: „A1:A500000”, press Enter then press Ctrl-D to fill the column with the formula down to row 500000. Do the same with the other 4 columns (my computer sent error message when I tried to fill the other 4 columns by dragging to the right).

Now you have 500000 rows, with five numbers in each row. Copy the whole range, open a new workbook and „Paste Special” the range you copied as „Value”.

Enter in F1:

=IF(AND(MAX(A1:E1)=C1,SUM(A1:E1)=100),C1,"")

and copy down. Now you get around 50 rows where the sum is 100 and column C has the highest value in the row. Copy this table next to it and „Paste special” as „Value”. By putting in order or by filtering, collect the rows which meet both conditions .

The data shows that around 1000000 „random rows” would produce 100 rows that meet both conditions, so you may have to repeat the procedure described above. With 500000 I got 46 such rows (the first 5 columns are the produced random numbers, the 6th colum shows the C value):
Excel Workbook
ABCDEF
126827152427
2131728182428
328232813828
432329291629
5112631201231
6202131121631
7101831212031
8181831161731
927732122232
1071332272132
113124363636
1216223619736
133443602636
1414153703437
157273722737
1623253831138
17181038142038
1814313821538
192643913039
2018641181741
211294123641
2212541201341
238244291742
2417224321643
2528154313143
260224333243
27101544171444
282922443244
291284520645
30419453245
3162546111246
32854733747
3310124724747
344347103647
35837481648
361328491949
374105203452
3820105331453
3910155451654
401920571357
41429592659
42032616161
431116123461
44524645264
45522653565
46102843184
Sheet
 
Upvote 0
Hi Istvan,

Nice "brute force" approach. Surely "success ratio" ca. 1 /10000 is not high :), but who cares, if we just rely on pure computing power. The distribution of results seem to be quite similar to these obtained by shg, especially for columns 1,2,4,5. Column 3 follows different pattern, with modal value near 40-45 long right tail and shorter left tail (obviously no chance for column 3 to fall below 20, because it wouldnt be highest out of 5 received from 100 division) :)

In the meantime I published the workbook for Ashwini as an attachment to Excel Club random numbers generated to sum up 2 (at very bottom of page). There is some overhead in the spreadsheet, so you can easily delete all after column W and also delete columns G:K.
 
Upvote 0
And what is the code for Make1D ?

Sorry:

Code:
Function Make1D(v As Variant, Optional iBase As Long = 0) As Double()
    ' Returns a 1D iBase-based array of the values in v, which can be a
    ' column or row vector range, a 1D or 2D array, or a scalar

    Dim adOut()     As Double
    Dim rArea       As Range
    Dim cell        As Range
    Dim nOut        As Long
    Dim i           As Long

    If IsArray(v) Then
        If TypeOf v Is Range Then
            ReDim adOut(iBase To v.Cells.Count - 1 + iBase)

            For Each rArea In v.Areas
                For Each cell In rArea.Cells
                    If VarType(cell.Value2) = vbDouble Then
                        adOut(nOut + iBase) = CDbl(cell.Value2)
                        nOut = nOut + 1
                    Else
                        Err.Raise CVErr(xlErrValue)
                    End If
                Next cell
            Next rArea
            Make1D = adOut

        Else    ' get the first column
            ReDim adOut(iBase To UBound(v) - LBound(v) + iBase)
            For i = LBound(v, 1) To UBound(v, 1)
                Select Case VarType(v(i))
                    Case vbInteger, vbLong, vbSingle, vbDouble, vbCurrency, vbDate, vbByte
                        adOut(nOut + iBase) = CDbl(v(i))
                    Case Else
                        Err.Raise CVErr(xlErrValue)
                End Select
                nOut = nOut + 1
            Next i
            Make1D = adOut
        End If

    Else
        ReDim adOut(iBase To iBase)
        Select Case VarType(v)
            Case vbInteger, vbLong, vbSingle, vbDouble, vbCurrency, vbDate, vbByte
                adOut(iBase) = CDbl(v)
            Case Else
                Err.Raise CVErr(xlErrValue)
        End Select
    End If
End Function
 
Upvote 0

Forum statistics

Threads
1,215,126
Messages
6,123,200
Members
449,090
Latest member
bes000

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