- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
这是我想要的功能
random_select(contain_list, ttl_num, sample_num)
从0
到ttl_num-1
有ttl_num
个整数可供选择,我想返回一个sample_num的列表
唯一整数,其中 contain_list
中提供的数字必须在列表中,其他数字随机选择。
我必须经常做这个查询,每次都有不同的contain_list
,但是ttl_num
, sample_num
对所有的都是一样的查询。
目前我在做的是,首先生成一组ttl_num
整数,从集合中减去contain_list
,随机选择一些数字,其余数字不替换,然后将其与 contain_list
连接以获得结果。
我认为这不是最快的方法,还有更好的想法吗?
如果需要,可以使用全局变量。
编辑:sample_num
不小于contain_list
长度,我想得到contain_list
加上sample_num - contain_list.length
其他随机数
保证 contain_list
中的数字都在 0
到 ttl_num-1
范围内。
最佳答案
这里有几种可能性。两者都没有您已有的复杂,但它们中的一个或两者可能会更快,具体取决于参数值的大小。只有根据您的实际数据进行基准测试才能确定。
此处的逻辑与您已经在做的基本相同。它只是用整数数组代替了集合的生成和操作,应该更轻量级。但是,它确实需要对 contain_list
进行排序(降序),因此它实际上是否比您已有的运行速度更快可能取决于 contain_list.count
的大小和 ttl_num
。
1) initialize a tracking var, remaining_num = ttl_num
2) initialize an integer array with value = index
3) sort contain_list descending
4) iterate through contain_list (now in descending order); for each:
4.1) decrement remaining_num
4.2) swap the element at the selected index with the one at index = remaining_num
5) iterate (sample_num - contain_list.count) times; for each:
5.1) generate a random index between 0 and remaining_num (inclusive and exclusive, respectively)
5.2) decrement remaining_num
5.3) swap the element at the selected index with the one at index = remaining_num
6) The resultant samples will start at index reamining_num and run through the end of the array.
这是一个运行 random_select({3, 7}, 10, 5)...的例子
remaining_num = 10
available_num[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
contain_list = {7, 3}
select the 7
remaining_num = 9
available_num[] = {0, 1, 2, 3, 4, 5, 6, 9, 8, 7}
select the 3
remaining_num = 8
available_num[] = {0, 1, 2, 8, 4, 5, 6, 9, 3, 7}
select a random(0,8), e.g. 2
remaining_num = 7
available_num[] = {0, 1, 9, 8, 4, 5, 6, 2, 3, 7}
select a random(0,7), e.g. 3
remaining_num = 6
available_num[] = {0, 1, 9, 6, 4, 5, 8, 2, 3, 7}
select a random(0,6), e.g. 0
remaining_num = 5
available_num[] = {5, 1, 9, 6, 4, 0, 8, 2, 3, 7}
result = {0, 8, 2, 3, 7}
如果 ttl_num
足够大而 sample_num
足够低,则可能值得将事情颠倒过来。也就是说,不是创建和操纵一组可用号码,而是仅跟踪选定号码的列表。然后,在选择每个随机目标时,通过遍历所选数字列表并计算小于或等于目标索引的次数来“跳过”先前选择的数字。
1) initialize a tracking var, remaining_num = ttl_num - contain_list.count
2) declare an empty list (vector) of integers, selected_num[]
4) iterate through contain_list; for each:
4.1) insert cointain_list[i] into selected_num[]
5) iterate (sample_num - contain_list.count) times; for each:
5.1) generate a random target between 0 and remaining_num (inclusive and exclusive, respectively)
5.2) decrement remaining_num
5.3) iterate through selected_num; for each:
5.3.1) if target >= selected_list[j], increment target
5.4) insert target into selected_num[]
6) The resultant samples will be all elements in selected_num.
这是一个运行 random_select({3, 7}, 10, 5)...的例子
remaining_num = 8
selected_num[] = {}
select the 3
selected_num[] = {3}
select the 7
selected_num[] = {3, 7}
select a random(0,8), e.g. target = 2
remaining_num = 7
2 < 3; target still 2
2 < 7; target still 2
selected_num[] = {3, 7, 2}
select a random(0,7), e.g. target = 3
remaining_num = 6
3 >= 3; target becomes 4
4 < 7; target still 4
4 >= 2; target becomes 5
selected_num[] = {3, 7, 2, 5}
select a random(0,6), e.g. target = 0
remaining_num = 5
0 < 3; target still 0
0 < 7; target still 0
0 < 2; target still 0
0 < 5; target still 0
selected_num[] = {3, 7, 2, 5, 0}
显然,如果 sample_num
很大,在选择每个新数字时遍历 selected_num[]
可能会变得很昂贵。这可以通过保持 selected_num[]
降序排列并在看到小于目标的数字时立即中断内部循环来缓解。在列表中的那个点插入目标以保持排序。
关于algorithm - 在包含一组给定数字的范围内随机选择一些数字的最快方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43088962/
我让随机数低于之前的随机数。 if Airplane==1: while icounter0: print "You have enoph fuel to get to New
是否可以生成 BigFloat 的随机数?类型均匀分布在区间 [0,1)? 我的意思是,因为 rand(BigFloat)不可用,看来我们必须使用 BigFloat(rand())为了那个结局。然而,
我正在尝试学习 Kotlin,所以我正在学习互联网上的教程,其中讲师编写了一个与他们配合良好的代码,但它给我带来了错误。 这是错误 Error:(26, 17) Kotlin: Cannot crea
是否有任何方法可以模拟 Collections.shuffle 的行为,而不使比较器容易受到排序算法实现的影响,从而保证结果的安全? 我的意思是不违反类似的契约(Contract)等.. 最佳答案 在
我正在创建一个游戏,目前必须处理一些math.random问题。 我的Lua能力不是那么强,你觉得怎么样 您能制定一个使用 math.random 和给定百分比的算法吗? 我的意思是这样的函数: fu
我想以某种方式让按钮在按下按钮时随机改变位置。我有一个想法如何解决这个问题,其中一个我在下面突出显示,但我已经认为这不是我需要的。 import javafx.application.Applicat
对于我的 Java 类(class),我应该制作一个随机猜数字游戏。我一直陷入过去几天创建的循环中。程序的输出总是无限循环,我不明白为什么。非常感谢任何帮助。 /* This program wi
我已经查看了涉及该主题的一些其他问题,但我没有在任何地方看到这个特定问题。我有一个点击 Web 元素的测试。我尝试通过 ID 和 XPath 引用它,并使用 wait.until() 等待它变得可见。
我在具有自定义类的字典和列表中遇到了该异常。示例: List dsa = (List)Session["Display"]; 当我使用 Session 时,转换工作了 10-20 次..然后它开始抛
需要帮助以了解如何执行以下操作: 每隔 2 秒,这两个数字将生成包含从 1 到 3 的整数值的随机数。 按下“匹配”按钮后,如果两个数字相同,则绿色标签上的数字增加 1。 按下“匹配”按钮后,如果两个
void getS(char *fileName){ FILE *src; if((src = fopen(fileName, "r")) == NULL){ prin
如果我有 2 个具有以下字段的 MySQL 数据库... RequestDB: - Username - Category DisplayDB: - Username - Category
我有以下语句 select random() * 999 + 111 from generate_series(1,10) 结果是: 690,046183290426 983,732229881454
我有一个使用 3x4 CSS 网格构建的简单网站。但出于某种原因,当我在 chrome“检查”中检查页面时,有一个奇怪的空白 显然不在我的代码中的标签。 它会导致网站上出现额外的一行,从而导致出现
我有两个动画,一个是“过渡”,它在悬停时缩小图像,另一个是 animation2,其中图像的不透明度以周期性间隔重复变化。 我有 animation2 在图像上进行,当我将鼠标悬停在它上面时,anim
如图所示post在 C++ 中有几种生成随机 float 的方法。但是我不完全理解答案的第三个选项: float r3 = LO + static_cast (rand()) /( static_c
我正在尝试将类添加到具有相同类的三个 div,但我不希望任何被添加的类重复。 我有一个脚本可以将一个类添加到同时显示的 1、2 或 3 个 div。期望的效果是将图像显示为背景图像,并且在我的样式表中
我有一个基本上可以工作的程序,它创建由用户设置的大小的嵌套列表,并根据用户输入重复。 但是,我希望各个集合仅包含唯一值,目前这是我的输出。 > python3 testv.py Size of you
我正在尝试基于 C# 中的种子生成一个数字。唯一的问题是种子太大而不能成为 int32。有什么方法可以像种子一样使用 long 吗? 是的,种子必须很长。 最佳答案 这是我移植的 Java.Util.
我写这个函数是为了得到一个介于 0 .. 1 之间的伪随机 float : float randomFloat() { float r = (float)rand()/(float)RAN
我是一名优秀的程序员,十分优秀!