生成随机序列时的尴尬标准
我需要做什么才能生成给定范围内满足我的特定标准的非重复整数序列?
标准如下:
- 仅使用 1 到 MAX 之间的数字(假设为 9)。
- 数字在序列中不能重复,除了:
2a。序列中前 5 个数字中的两个必须重复。
2b。这两个数字必须在最终序列的最后 5 个位置内的随机点上重复(最后 5 个位置包括重复)。
例如: 设置:1,2,3,4,5,6,7,8,9
随机序列(带重复):2,4,6,9,3,1,5,2,8,7,3
r, , , ,r, , ,x, , ,x
这里我用 表示随机选择要重复的数字(随机序列中的前 5 个数字) r
以及用 x
随机放置它们的插入点(最终序列的最后 5 个)。
非常感谢任何帮助解决这个问题的帮助。实际使用会比这更复杂一些,但我知道一旦我能做到这一点我需要做什么。
编辑
为了澄清一点,我有 1-20,并且我需要一个 22 位随机序列。每个数字都必须使用,两个数字将使用两次,正如我在原始帖子中讨论的那样。我选择上面的10来简化一点。我应该能够适应你们所给出的逻辑。
What I need to do to generate a sequence of non-repeating integers within a given range that meets the specific criteria that I have?
Here are the criteria:
- Use only the numbers between 1 and MAX (let's say 9).
- Numbers cannot repeat within the sequence except:
2a. Two of the first 5 numbers from the sequence must be repeated.
2b. These two numbers must be repeated at random points within the last 5 places in the final sequence (the last 5 includes the repeats).
For example:SET: 1,2,3,4,5,6,7,8,9
Random Sequence (with repeats):2,4,6,9,3,1,5,2,8,7,3
r, , , ,r, , ,x, , ,x
Here I have indicated the numbers that were randomly selected to be repeated (out of the first 5 in the random sequence) with an r
and the insertion points where they were randomly placed (into the last 5 of the final sequence) with an x
.
Any help in figuring this out is much appreciated. Actual use will be a bit more complicated than this, but I know what I will need to do once I can get this far.
Edit
To clarify a little more, I have 1-20, and I need a 22 digit random sequence. Every number must be used, two will be used twice as discussed in my original post. I chose 10 above to simplify a little. I should be able to adapt the logic you've all given.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我假设当你说“不重复”时,你的意思是“不同”(唯一)而不是“最终变成周期性”(如“圆周率的数字不重复”)
删除子列表不是必需的,但可以更容易概念化。
如果 n+2 小于 10,该怎么办并不明显。特别是,当 n < 10 时,该算法可能会崩溃。 5 并返回 n=7 的错误结果。
I assume when you say "non-repeating" you mean "distinct" (unique) as opposed to "eventually becomes periodic" (as in "the digits of pi do not repeat")
Removal of the sublist is not necessary but makes it easier to conceptualize.
Not obvious what to do if n+2 is less than 10. In particular, this algorithm may crash for n < 5 and return the wrong result for n=7.
如果我理解正确的话,您有 1 到 N 个随机数,必须在 10 组排列中使用,并具有一些关于重复的特定标准。在 php 中,我建议这个(不包括 php-internals)O(n) 解决方案:
没有循环,没有条件。
If I understand you correctly, you have 1 to N random numbers that must be used in a 10-set permutation with some specific criteria about repeats. In php, I suggest this (not counting php-internals) O(n) solution:
No loops, no conditionals.
关于此解决方案的警告。我不会将它用于大量数字。如果我对更大的集合执行相同的解决方案,我将使用 array_splice 从数组中删除选定的成员。当您获得更大的空间时,在范围内找到未使用的数字变得相当昂贵,并且需要比下面的强力方法更好的解决方案。
这将构建您目标集的一半。你会叫它两次,每半场一次。
这将从数组中获取 $this_many 元素。
这是一个相当特殊的情况,但可以通过允许将偏移地板和天花板作为参数传递来变得更通用。对于你的问题,它们是 5 和 9,所以我们直接导出它们。
好的,完成所有这些之后,让我们开始工作吧。
A word of warning on this solution. I wouldn't use it for a large set of numbers. If I were doing this same solution for a much larger set, I would use array_splice to drop chosen members from the array. As you get a much larger space, finding an unused number in your range becomes quite expensive, and demands a better solution than the brute force method below.
This will build half of your target set. You will call it twice, once for each half.
This will grab $this_many elements from the array.
This is a fairly specialized case, but could be made more general by allowing the offset floor and ceiling to be passed in as parameters. For your problem they would be 5 and 9, so we just derive them directly.
Ok so after having done all that, let's put it to work.
希望这能让你上路:
Hope this gets you on your way:
要在某个范围内生成不同的数字,您可以使用如下内容:
$arr_num 现在有 8 个不同的元素。选择数组的五个元素:
现在从 $new_arr 数字中选择两个数字:
现在您可以将这些数字插入到原始数组的最后两个随机位置。希望有帮助!
To generate distinct numbers within a range you can use something like this:
$arr_num now has 8 distinct elements. Pick five elements of the array:
Now pick two numbers from $new_arr numbers:
Now you can insert these numbers into the original array at two of the last random positions. Hope it helped!