用最小间隔/分布填充随机数数组?
我正在尝试用随机数填充数组,但随机数之间必须有一定的最小距离。
我已经用 0 - 100 之间的 5 个随机数填充了一个数组:
private var myArray:Array = new Array();
for (var i:uint = 0; i < 5; i++)
myArray.push(Math.round(Math.random() * 100));
接下来我按数字顺序对数组进行了排序:
myArray.sort(Array.NUMERIC);
填充和排序后,让我们假设 myArray 现在包含这些值:
26, 27, 42, 92, 97
现在我想如果需要,某些或所有数组值都将被重置,以便它们彼此之间至少有最大值 (100) 的一定百分比(假设为 10%)。
前 2 个值(26 和 27)相差不至少 10%,后 2 个值(92 和 97)也相差不到 10%。但是,如果我只是将值 27 从 26 移开 10%,那么 27 就会更改为 37,那么 37 现在与下一个值 42 发生冲突。
填充随机数数组的最佳方法是什么,其值至少为彼此之间有一定的百分比,但仍然是随机的。
这可能不言而喻,但我正在寻找一种可移植的解决方案,其中最大值和最小分配百分比可以是任何值,而不仅仅是我上面的示例。
i'm attempting to populate an array with random numbers, but the random numbers must be a certain, minimum distance apart from each other.
i've populated an array with 5 random numbers between 0 - 100:
private var myArray:Array = new Array();
for (var i:uint = 0; i < 5; i++)
myArray.push(Math.round(Math.random() * 100));
next i've sorted the array in numeric order:
myArray.sort(Array.NUMERIC);
after populating and sorting let's assume that myArray now contains these values:
26, 27, 42, 92, 97
now i would like some ore all of the array values to be reset, if they need to be, so that they are at least a certain percentage (let's say 10%) of the maximum value (100) apart from each other.
the first 2 values (26 and 27) are not at least 10% apart and neither are the last 2 values (92 and 97). however, if i simply moved the value 27 10% away from 26, so that 27 changes to 37, 37 now conflicts with the next value of 42.
what is the best approach for populating an array of random numbers who's values will be at least a certain percentage apart from one another but still random.
it may go without saying, but i'm looking for a solution that is portable, where maximum values and minimum distribution percentages can be anything, not just for my example above.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在您的情况下,您需要 5 个号码,彼此之间至少有 10 个号码。您应该做的是创建从 0 到 60 的 5 个数字,对它们进行排序,然后开始添加分隔符。因此,如果您的原始列表是
26, 27, 42, 52, 57
那么您的新列表是26、27+10、42+20、52+30、57+40
或26、37、62、82、97
。这可以推广到任何所需的范围、元素数量和分隔符的大小。如果您想要 n 个元素至少有 d 将它们划分在 x 到 y 的范围内,则在 (x, y - (n-1)*d) 范围内填充 n 个元素,对它们进行排序,然后开始添加分隔符。
In your case you want 5 numbers, all at least 10 from each other. What you should do is create 5 numbers from 0 to 60, sort them, then start adding the dividers in. So if your original list is
26, 27, 42, 52, 57
then your new list is26, 27+10, 42+20, 52+30, 57+40
or26, 37, 62, 82, 97
.This can be generalized to any desired range, number of elements and size of divider. If you want n elements with at least d dividing them in a range from x to y, then populate n elements in the range (x, y - (n-1)*d), sort them, and start adding the dividers in.
如果您知道每个变量值的百分比增量(例如 10%)和最小值(0%、20%、40%、60%、80%),您还知道 100% 值(我们称之为基数 = 100),那么您将生成如下数组:
var array: Array = new Array(Math.round(Math.random() * 0.1 * base), Math.round(Math.random() * 0.1 * base) + 0.2 * 基数,Math.round(Math.random() * 0.1 * 基数) + 0.4 * 基数,Math.round(Math.random() * 0.1 * 基数) + 0.6 * 基数,Math.round(Math.random( ) * 100) + 0.8 * 0.1 * 基数);
if you know percentage delta for each variable value (e.g. 10%) and minimumum value (0%, 20%, 40%, 60%, 80%), you also know 100% value (let's call it base = 100), then you'll generate you array like this:
var array: Array = new Array(Math.round(Math.random() * 0.1 * base), Math.round(Math.random() * 0.1 * base) + 0.2 * base, Math.round(Math.random() * 0.1 * base) + 0.4 * base, Math.round(Math.random() * 0.1 * base) + 0.6 * base, Math.round(Math.random() * 100) + 0.8 * 0.1 * base);