在生成随机整数序列,无需构建整个名单前期整数、序列、名单

2023-09-11 05:14:36 作者:不羁

我怎样才能生成一个整数从1到N,但随机顺序名单,而没有构建在内存中的整个列表?

How can I generate the list of integers from 1 to N but in a random order, without ever constructing the whole list in memory?

(需要明确的是:在生成的列表中的每个数字只能出现一次,所以它必须是相当于创建整个列表在内存中,再洗牌)

(To be clear: Each number in the generated list must only appear once, so it must be the equivalent to creating the whole list in memory first, then shuffling.)

此已被确定为是的this问题。

推荐答案

非常简单随机是1 +((电源(R,X)-1)模p)将是从1到p为x的值从1到p和将是随机的,其中r和p为素数,为r>页。

very simple random is 1+((power(r,x)-1) mod p) will be from 1 to p for values of x from 1 to p and will be random where r and p are prime numbers and r <> p.