简体   繁体   中英

How to randomly distribute a known group of numbers into a column using Excel / VBA

I'm stuck with excel/vba:

I've got a 10 row x 30 column blank array in Excel. I am trying to distribute 10 integers from a known group of 10 (say 1,1,1,1,1,1,3,5,7,9) into each column randomly so that each row of the column contains one of the group (and all of the group members are used once), and I need the second column to contain another random distribution of the same group and so on.

So I'd end up with 30 columns of 10 rows each, with each column containing a different random distribution of the same 10 integers. I want to be able to change the distribution in each row by recalculating the spreadsheet too.

Is there a quick way to do this? Short of arranging 30 different rand() sorted lists and using lookups I couldn't see a way. I'm not savvy enough with VBA to have a go. If someone can point me in the right direction, I'd be eternally grateful!

You could make a loop in which you make an array with your 10 numbers. Then loop though 30 columns, with first adding another column of 10 randomly drawn numbers to your array. See this website on how to draw random numbers . Then sort the array on the second column and post the first column.

Edit:

As I read in the comments on the other answer, the purist solution would be to:

  1. Assign each unique option of values a random value
  2. Sort these random values either from top to bottom or bottom to top, and select the top one.
  3. Place it in the first row
  4. Do the same thing again for the second row, but keep track of the sum of all the unique options, as to rule out an option once it maxed its presence.

Edit2:

Once I just clicked post I thought this a bit more through and came to the conclusion that the last digit will allmost always be 1 in this case....

Perhaps I'm missing something obvious, though this does not seem to be so straightforward using worksheet formulas alone.

If your orginal list of values is in A1:A10, then, in B1:

=INDEX($A$1:$A$10,RANDBETWEEN(1,10))

and in B2, array formula** :

=INDEX($A$1:$A$10,INDEX(MODE.MULT(IF(COUNTIF($A$1:$A$10,$A$1:$A$10)-COUNTIF(B$1:B1,$A$1:$A$10),{1,1}*ROW($A$1:$A$10))),RANDBETWEEN(1,10-ROWS($1:1))))

Copy the above down to B10.

You can then copy the formulas in B1:B10 to the right as desired.

Regards

**Array formulas are not entered in the same way as 'standard' formulas. Instead of pressing just ENTER, you first hold down CTRL and SHIFT, and only then press ENTER. If you've done it correctly, you'll notice Excel puts curly brackets {} around the formula (though do not attempt to manually insert these yourself).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM