Java 生成2^30的随机数

Java 生成2^30的随机数,java,c++,c,Java,C++,C,我想生成0到2^10范围内的2^30随机数。我听说,rand()函数不适合这么多的数字。有没有其他方法可以生成分布几乎相等的函数?Mark A。奥弗顿在2011年5月24日发表了一篇关于相当简单但高质量的RNG的文章。在Java中,您可以使用随机函数,它在2^48个值之后重复 Random rand = new Random(); for(int i = 0; i < (1<<30); i++) { int n = rand.nextInt(1 << 10

我想生成0到2^10范围内的
2^30
随机数。我听说,
rand()
函数不适合这么多的数字。有没有其他方法可以生成分布几乎相等的函数?

Mark A。奥弗顿在2011年5月24日发表了一篇关于相当简单但高质量的RNG的文章。

在Java中,您可以使用随机函数,它在2^48个值之后重复

Random rand = new Random();

for(int i = 0; i < (1<<30); i++) {
    int n = rand.nextInt(1 << 10);

}
Random rand=new Random();
对于(int i=0;i <(1 < P> C++ >代码> <代码>库)是一个很好的选择,具有多种PRNG引擎和分布的选择。
#include <random>
#include <cstdint>
#include <iostream>

int main() {
    std::random_device r;
    std::seed_seq seed{r(), r(), r(), r(), r(), r(), r(), r()};
    std::mt19937_64 eng(seed);
    std::uniform_int_distribution<> dist(0, 1<<10);

    for (std::uint32_t i = 0; i< (1<<30); ++i) {
        int value = dist(eng);
        std::cout << value << ' ';
    }
}
#包括
#包括
#包括
int main(){
std::随机_装置r;
种子{r(),r(),r(),r(),r(),r(),r(),r(),r(),r(),r(),r()};
标准:mt19937_64英语(seed);

标准:均匀分布距离(0,1增加随机性和周期的简单方法:

public class Random2 {

    private static int LEN = 64;
    private final int[] buf = new int[LEN];
    private Random r;
    private final int maxInt = 1 << 10;

    public Random2() {
        r = new Random();
        for (int i = 0; i < LEN; i++)
            buf[i] = r.nextInt(maxInt);
    }

    public int nextInt() {
        int i = r.nextInt(LEN);
        int x = buf[i];
        buf[i] = r.nextInt(maxInt);
        return x;
    }

}
公共类随机数2{
私有静态int LEN=64;
私有最终整数[]buf=新整数[LEN];
私有随机r;
私有最终整数maxInt=1
g_random_int()返回在[0..2^32-1]范围内均匀分布的随机guint32

编辑:

直接从/dev/random(可移植性较差)读取,按常规编译:

#include <stdio.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>

int
main(void)
{
    int             fd;
    unsigned int    number;

    fd = open("/dev/random", O_RDONLY);

    read(fd, &number, sizeof(number));

    printf("%u\n", number);
    close(fd);
  return 0;
}
#包括
#包括
#包括
#包括
int
主(空)
{
int-fd;
无符号整数;
fd=打开(“/dev/random”,仅限ordu);
读取(fd和编号,sizeof(编号));
printf(“%u\n”,数字);
关闭(fd);
返回0;
}

PS:检查错误。

这是一篇旧的Usenet文章,其中有许多有趣的RNG——所有这些都很容易实现

它们可能与Mersenne twister不太匹配,但我已经很好地使用了其中的几个,它们肯定优于一些默认的rand()实现。它们通过了严格的随机性测试,其中最大的周期生成器的周期>2^7700,并且只需几行即可实现


Ken

查看C++11的
标题。在C++03中,Boost.Random(其中
来自哪里)。生成均匀分布的大随机数不是有问题吗?说真的吗?
java
c++
c
?你的问题与所有这些都相关吗?实际上任何语言都适合我,我只想将它重定向到文件!!如果是c/c++那么最好。rand.c是我存储代码的文件吗?(对不起,我对编者的了解很少)@jhon如果你只想要0到2^32之间的随机数,那么只要从/dev/random中读取它就会给你带来随机性:P在你的shell中试试这个:
od-An-N4-tu/dev/random-tr-d'
重定向到一个文件
od-An-N4-tu/dev/random | tr-d'>file.txt
,但是如果你将上面的代码复制并粘贴到一个文件调用中ed rand.c您实际上可以通过输入上面的命令来编译它。请注意,代码有一个要求,您需要在系统中安装glib。当然,如果您不使用Linux,这一切都不会起作用。好的,感谢shell命令,我可以这样做。但是当我使用上面的命令为文件rand.c编译时,它会出现符号>>。哦,我们LH…我希望shell命令能满足你的需要。如果不能,你可以再问一个关于如何编译代码的问题,这很简单。但是我真的建议你使用这个命令,而不是其他的C/C++/Java代码,因为/dev/random会产生真正的随机数。好了,现在用C调用上面的脚本,我使用的是system(“/path/myscript.sh”)。它按预期给出输出,但如何在变量中重定向它。例如:int abc=system(“/path/myscirpt.sh”)?您使用的编译器/库和版本是什么?它可能还没有实现。您也可以尝试
std::mt19937
或其他方法之一。@john您必须添加
-std=c++0x
编译器选项
#include <glib.h>

int
main(void)
{
     g_print("%d\n", g_random_int());
   return 0;
}
gcc -o rand rand.c `pkg-config --cflags --libs glib-2.0`
#include <stdio.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>

int
main(void)
{
    int             fd;
    unsigned int    number;

    fd = open("/dev/random", O_RDONLY);

    read(fd, &number, sizeof(number));

    printf("%u\n", number);
    close(fd);
  return 0;
}