python - 在 cython 中生成随机整数的最快方法?
问题描述
我正在编写一个在任何地方都使用随机行为的程序。
为了说明,我的代码如下:
cdef int r = 8, c = 14
cdef class myclass1:
pass
many_functions = [f1, f2, ..., f20, ...]
# Each functions are quite different in code structures,
# so I can't just generate one big random sequence and use them
cdef myclass1 x
old_container = [...] # contains many myclass1 instances
for _ in range(1000): #main loop
new_container = []
for x in old_container:
func = random.choice(many_functions)
new_container.append(func(x))
old_container = new_container
# an example for f
cdef myclass1 f_n(myclass1 x):
cdef int a, b, c
a = random.randint(r)
b = random.randint(c)
c = random.randint(rc)
# do something and return x
目前,numpy.random.Generator.integers
每当我需要一个或多个随机整数时,我都会使用它。
cdef int m
m = rng.integers(r)
我不确定这是最好的方法。即使我每次需要少量兰特,也可以使用 numpy.random 吗?在纯 python 中只使用 random.randint 会更好吗?或者我还有其他更好的选择吗?
*编辑:速度是我唯一关心的。对不起,含糊的问题。
解决方案
推荐阅读
- java - 有什么方法可以知道 Android 上的 Parcel 中的对象类型是什么?
- git - K8S:如何通过 SSH 服务(Gitea)在同一主机上访问 pod,ingress-nginx 暴露为 HTTP?
- python - Groupby多年来每个月的最大值和最小值
- java - 如何从 eclipse e4 中的处理程序执行方法中获取图标
- javascript - JWT 验证错误:JsonWebTokenError: invalid algorithm
- equals - 两个字面上相同的 CGAL::Point_3 点之间不相等
- python-3.x - 数据提取到图形输出
- java - JUnit 5 @Tag does not work as expected with @BeforeEach/@AfterEach
- mongodb - 在 Mono 对象上执行 block() 时出现异常我从 ReactiveMongoRepository 对象返回
- java - ORA-28040: 没有匹配的身份验证协议 Oracle