简体   繁体   English

如何使用Excel / VBA将已知数字组随机分配到列中

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

I'm stuck with excel/vba: 我坚持使用excel / vba:

I've got a 10 row x 30 column blank array in Excel. 我在Excel中有一个10行x 30列的空白数组。 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. 我正在尝试从已知的10组(例如1,1,1,1,1,1,3,5,7,9)中随机分配10个整数,以便该列的每一行都包含一个组(所有组成员都使用一次),并且我需要第二列包含同一组的另一个随机分布,依此类推。

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. 因此,我最终得到30列,每列10行,每列包含相同的10个整数的不同随机分布。 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. 没有安排30个不同的rand()排序列表并使用查找,我看不到任何方法。 I'm not savvy enough with VBA to have a go. 我对VBA不够了解,无法尝试。 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. 您可以创建一个循环,在其中创建包含10个数字的数组。 Then loop though 30 columns, with first adding another column of 10 randomly drawn numbers to your array. 然后循环遍历30列,首先将另一列10个随机绘制的数字添加到数组中。 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: 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.... 一旦我点击了帖子,我就想通了一点,得出的结论是,在这种情况下,最后一位将始终为1。

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: 如果您的原始值列表在A1:A10中,那么在B1中:

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

and in B2, array formula** : 在B2中, 数组公式**

=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. 将以上内容复制到B10。

You can then copy the formulas in B1:B10 to the right as desired. 然后,您可以根据需要将B1:B10中的公式复制到右侧。

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. 首先按住CTRL和SHIFT,然后再按Enter,而不是仅按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). 如果操作正确,您会注意到Excel在公式周围使用大括号{}(尽管不要尝试自己手动插入大括号)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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