[英]How do I create a list of non-repeating cells/numbers in Google Sheets?
I'm trying to emulate Minesweeper in Google Sheets, and for this I want to create a second map adjacent to the first with all of the correct values already in it.我正在尝试在 Google 表格中模拟 Minesweeper,为此我想创建第二个 map 与第一个相邻,其中包含所有正确的值。 To randomize bomb position, I need a list of random numbers or cells(cells would be preferable).
要随机化炸弹 position,我需要一个随机数或单元格列表(最好是单元格)。 However, I cannot figure out how to do this without ending up repeating numbers.
但是,如果不结束重复数字,我无法弄清楚如何做到这一点。 The result would ideally be a vertical array of cell coordinates.
理想情况下,结果将是单元格坐标的垂直数组。 Thank you!
谢谢你!
The following formula should produce the result you desire:以下公式应该会产生您想要的结果:
=SORTN(FLATTEN(MAKEARRAY(10,10,LAMBDA(row,col,ADDRESS(row,col)))),20,,RANDARRAY(100),)
In =MAKEARRAY
, change the first 10
to adjust how many rows to randomly choose from, or the second 10
to adjust how many columns to choose from.在
=MAKEARRAY
中,更改前10
以调整要从中随机选择的行数,或更改第二个10
以调整要从中选择的列数。 The value in =RANDARRAY
must be equal to the product of the number of rows and the number of columns. =RANDARRAY
中的值必须等于行数和列数的乘积。 (eg in the above example, 10*10=100). (例如,在上面的例子中,10*10=100)。
Change the 20
to adjust how many randomly chosen values to return.更改
20
以调整要返回的随机选择值的数量。
=MAKEARRAY
is used to generate an array of every possible row and column combination. =MAKEARRAY
用于生成每个可能的行和列组合的数组。 It accepts a =LAMBDA
, which in this case is just the =ADDRESS
function. The first two arguments of =MAKEARRAY
determine how large the array should be, which is why changing them adjusts how many rows/columns to randomly pick from.它接受一个
=LAMBDA
,在本例中它只是=ADDRESS
function。 =MAKEARRAY 的前两个=MAKEARRAY
确定数组应该有多大,这就是为什么更改它们会调整要从中随机选择多少行/列的原因。
Then, the result of =MAKEARRAY
is squashed into a single column using the =FLATTEN
formula.然后,使用
=FLATTEN
公式将=MAKEARRAY
的结果压缩到单个列中。
Finally, the entire thing is sorted randomly using =SORTN
combined with =RANDARRAY
.最后,使用
=SORTN
结合=RANDARRAY
对整个事物进行随机排序。 =SORTN
also limits the number of results that are returned dependent on its second argument, which is why changing it adjusts how many results are returned. =SORTN
还根据其第二个参数限制返回的结果数,这就是为什么更改它会调整返回的结果数。
If you want information on how to "freeze" the value of =RANDARRAY
so it doesn't recalculate each time you change something, check out this question by player0.如果您想了解如何“冻结”
=RANDARRAY
的值,这样它就不会在您每次更改内容时都重新计算,请查看 player0 提出的这个问题。
Functions used:使用的函数:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.