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
 
Hello,

I suggest to create a new sheet "Random_01_Array" in the file I provided the above link for, and then enter:
sbExactRandHistogrm_MrExcel_Simple_Problem.xlsm
ABCDEFGHIJKLMNOPQRSTUVWX
1Min Half Days for Weekly Attendance3
2Min Half Days for Bi-Weekly Attendance10
3BiWeekly (%)0,3
4Weekly (%)0,7
5MorningAfternoonMorningAfternoonMorningAfternoonMorningAfternoonMorningAfternoonMorningAfternoonMorningAfternoonMorningAfternoonMorningAfternoonMorningAfternoon
6MembersAttendanceDaysMonMonTueTueWedWedThuThuFriFriMonMonTueTueWedWedThuThuFriFriSum
71Weekly140110110110111011101114
82Biweekly131001110110011011111013
93Biweekly121001110110111000011112
104Weekly161111110101111100111116
115Biweekly121111110100001101110012
126Weekly111001011010011001111011
137Weekly201111111111111111111120
148Weekly4000000001101000000104
159Weekly6000000100011100100106
1610Weekly131101010100111110111013
1711Weekly2000000100000000010002
1812Weekly161110111110111101011116
1913Weekly4001000001000100000104
2014Weekly6100110001011000000006
2115Biweekly171011101111111011111117
2216Biweekly130110111110111001100113
2317Weekly121010000111111110001112
2418Weekly150101111101110011111115
2519Biweekly100010110110110110010010
2620Weekly111100011110101000101111
2721Weekly181101111111111101111118
2822Weekly3000100100001000000003
2923Biweekly1000000000000001000001
3024Biweekly110100110111001011001111
3125Weekly8100100111000101100008
3226Weekly150111111001111100111115
3327Weekly3000000000001000010103
3428Biweekly191111111111101111111119
3529Weekly130101101110010111011113
3630Weekly101011010000110001011110
Random_01_Array
Cell Formulas
RangeFormula
B4B4=1-B3
B7:B36B7=TRANSPOSE(CHOOSE(INT(sbExactRandHistogrm(30,1,3,B3:B4)),"Biweekly","Weekly"))
C7:C36C7=IF(C9="Weekly", RANDBETWEEN($B$2, 10), RANDBETWEEN($B$3, 20))
D7:W36D7=INT(sbExactRandHistogrm(20,0,2,SEQUENCE(1,2,20-C7,20-2*(20-C7))))
X7:X36X7=SUM(D7:W7)
Dynamic array formulas.


Regards,
Bernd
 
Upvote 0
Solution

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Hello,

I suggest to create a new sheet "Random_01_Array" in the file I provided the above link for, and then enter:
sbExactRandHistogrm_MrExcel_Simple_Problem.xlsm
ABCDEFGHIJKLMNOPQRSTUVWX
1Min Half Days for Weekly Attendance3
2Min Half Days for Bi-Weekly Attendance10
3BiWeekly (%)0,3
4Weekly (%)0,7
5MorningAfternoonMorningAfternoonMorningAfternoonMorningAfternoonMorningAfternoonMorningAfternoonMorningAfternoonMorningAfternoonMorningAfternoonMorningAfternoon
6MembersAttendanceDaysMonMonTueTueWedWedThuThuFriFriMonMonTueTueWedWedThuThuFriFriSum
71Weekly140110110110111011101114
82Biweekly131001110110011011111013
93Biweekly121001110110111000011112
104Weekly161111110101111100111116
115Biweekly121111110100001101110012
126Weekly111001011010011001111011
137Weekly201111111111111111111120
148Weekly4000000001101000000104
159Weekly6000000100011100100106
1610Weekly131101010100111110111013
1711Weekly2000000100000000010002
1812Weekly161110111110111101011116
1913Weekly4001000001000100000104
2014Weekly6100110001011000000006
2115Biweekly171011101111111011111117
2216Biweekly130110111110111001100113
2317Weekly121010000111111110001112
2418Weekly150101111101110011111115
2519Biweekly100010110110110110010010
2620Weekly111100011110101000101111
2721Weekly181101111111111101111118
2822Weekly3000100100001000000003
2923Biweekly1000000000000001000001
3024Biweekly110100110111001011001111
3125Weekly8100100111000101100008
3226Weekly150111111001111100111115
3327Weekly3000000000001000010103
3428Biweekly191111111111101111111119
3529Weekly130101101110010111011113
3630Weekly101011010000110001011110
Random_01_Array
Cell Formulas
RangeFormula
B4B4=1-B3
B7:B36B7=TRANSPOSE(CHOOSE(INT(sbExactRandHistogrm(30,1,3,B3:B4)),"Biweekly","Weekly"))
C7:C36C7=IF(C9="Weekly", RANDBETWEEN($B$2, 10), RANDBETWEEN($B$3, 20))
D7:W36D7=INT(sbExactRandHistogrm(20,0,2,SEQUENCE(1,2,20-C7,20-2*(20-C7))))
X7:X36X7=SUM(D7:W7)
Dynamic array formulas.


Regards,
Bernd
Bernd, thank you so much. This is a very elegant solution and it works flawlessly for any inputs. Your custom functions should be standard for MS excel.
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,152
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