在生成随机整数序列,无需构建整个名单前期 [英] Generate sequence of integers in random order without constructing the whole list upfront

查看:144
本文介绍了在生成随机整数序列,无需构建整个名单前期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能生成一个整数从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.)

此已被确定为是的<一个重复href="http://stackoverflow.com/questions/464476/generating-shuffled-range-using-a-prng-rather-than-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.

这篇关于在生成随机整数序列,无需构建整个名单前期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆