Random 0/1 array with set numbers of 1s

Augustinn

New Member
Joined
Sep 28, 2022
Messages
6
Office Version
  1. 365
Platform
  1. Windows
I'm trying to generate a random distribution of 0's and 1's to a 2x10 array with the condition that a number of 1's must be a set number, let's say 15.
Here is an exact example of what I'm trying to accomplish:
- Student will be attending RANDOM 15 classes (or any number between 1 and 20)
- There are two classes per day, one in the morning, the second in the evening
- Classes will be held in 10 consecutive days
- Since student will be attending only 15 out of total 20 classes, 5 classes need to be randomly skipped
- Generate 2x10 array with the conditions above
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
This will do what you have asked for, please note that all steps must be carried out correctly and in the order shown otherwise it is likely to fail.

First of all (in Excel) go to File > Options > Formulas and check the box to Enable iterative calculation (top right of the options list).

Next, enter this formula into a single cell which should be the top left cell of a 2x10 array of empty cells.

Note that the 2 references to A2 in the formula must be changed to the cell which you are entering the formula into.
Excel Formula:
=IF(SUM(A2#)=15,A2#,RANDARRAY(2,10,0,1,1))
Be sure not to delete the # symbols when editing the cell references.
 
Upvote 0
Hello,

I suggest to use this:

Regards,
Bernd
 
Upvote 0
This will do what you have asked for, please note that all steps must be carried out correctly and in the order shown otherwise it is likely to fail.

First of all (in Excel) go to File > Options > Formulas and check the box to Enable iterative calculation (top right of the options list).

Next, enter this formula into a single cell which should be the top left cell of a 2x10 array of empty cells.

Note that the 2 references to A2 in the formula must be changed to the cell which you are entering the formula into.
Excel Formula:
=IF(SUM(A2#)=15,A2#,RANDARRAY(2,10,0,1,1))
Be sure not to delete the # symbols when editing the cell references.
Thanks for helping.
It works but only for some values. It's weird. It will work for values between 6 and 15, but not above 15 or below 6. I modify the formula to array 1:20, but it does not work with 2:10 either. See the last column. Greens are OK, rest are wrong. Any suggestions?

Spartans.xlsx
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABAC
2Min Half Days for Weekly Attendance3
3Min Half Days for Bi-Weekly Attendance10
4BiWeekly (%)30%
5
6MorningAfternoonMorningAfternoonMorningAfternoonMorningAfternoonMorningAfternoonMorningAfternoonMorningAfternoonMorningAfternoonMorningAfternoonMorningAfternoon
7MembersAttendanceDaysMonMonTueTueWedWedThuThuFriFriMonMonTueTueWedWedThuThuFriFri
8
91BiWeekly170110001111100100101111
102BiWeekly19000110101101011000109
113BiWeekly131100001111111010011113
124BiWeekly201111001011011111010013
135BiWeekly15111110000000000011018
146BiWeekly161010011011000100111111
157BiWeekly110100110010101011011111
168BiWeekly201110111001111100110114
179Weekly8001000101100010001118
1810Weekly4001001010100000000004
1911Weekly8010100111000100110008
2012Weekly50110011010001001111111
2113Weekly9011000111001001010109
2214Weekly7100000100010101000117
2315Weekly8001111010000010010018
2416Weekly5000100111000100000005
2517Weekly41000101010011110101111
2618Weekly6001100001110010000006
2719Weekly8011000110000100100118
2820Weekly8001010001110101010008
2921Weekly6000001000000011011016
3022Weekly5000110010001010000117
3123Weekly100111001110100100011010
3224Weekly9011101001001010001019
3325Weekly5000000101111000000005
3426Weekly8100110001110000011008
3527Weekly30000001010110111101110
3628Weekly5100000000000001110015
3729Weekly7011001001001010010007
3830Weekly6000010001010000010116
Sheet1
Cell Formulas
RangeFormula
D9:D38D9=IF(B9<(30*$D$4), "BiWeekly", "Weekly")
E9:E38E9=IF(D9="Weekly", RANDBETWEEN($D$2, 10), RANDBETWEEN($D$3, 20))
H9:AA38H9=IF(SUM(H9#)=$E9,H9#,RANDARRAY(1,20,0,1,1))
AC9:AC38AC9=SUM(H9:AB9)
Dynamic array formulas.
Cells with Conditional Formatting
CellConditionCell FormatStop If True
AC9:AC108Cell Value=$E9textNO
 
Upvote 0
It will work for values between 6 and 15
It's working for me from 4 to 17, bearing in mind that at the extremities or the range you are looking at more of a bias toward one of the 2 options than a random selection.

Also, the original question asked for a random selection based on what appeared to be a defined number to choose from a pool (15 from 20 was the primary example). I haven't done extensive testing but it appears that the randbetween formula in column E reduces the chance of the array returning the desired result.

You could try going back to the options tab and increasing the number of iterations, this should improve the chance of the correct results but the increased number of calculation cycles would likely come with a cost of increased processing time.
 
Upvote 0
It's working for me from 4 to 17, bearing in mind that at the extremities or the range you are looking at more of a bias toward one of the 2 options than a random selection.

Also, the original question asked for a random selection based on what appeared to be a defined number to choose from a pool (15 from 20 was the primary example). I haven't done extensive testing but it appears that the randbetween formula in column E reduces the chance of the array returning the desired result.

You could try going back to the options tab and increasing the number of iterations, this should improve the chance of the correct results but the increased number of calculation cycles would likely come with a cost of increased processing time.
Unfortunately, even with Maximum iterations at 10000, only few results match. Any other ideas?
 
Upvote 0
My results seem to be a bit closer than yours were and I only set it to 1000 so I guess it means that the number of matching results from a random array is random. I don't see that you will be able to make it any more accurate with formulas alone, have you looked at @Sulprobil's alternative suggestion?
 
Upvote 0
I'm afraid that I'm out of ideas then, the method that I suggested worked for me in simplified testing based on the original question but it seems unable to cope with the scope of the variables required for the full task.
 
Upvote 0

Forum statistics

Threads
1,224,814
Messages
6,181,121
Members
453,021
Latest member
Justyna P

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