我应该为C中的erand48()使用什么种子值? [英] What seed value should I use for erand48() in C?

查看:647
本文介绍了我应该为C中的erand48()使用什么种子值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C语言编程的新手,并且我已经读过erand48()是线程安全随机数生成的一个不错的选择。但是该函数的种子值为:unsigned short int array [3]

I am new to C programming, and i've read that erand48() is a good option for thread-safe random number generation. However the function takes a seed value of: unsigned short int array[3]

关于此种子值应初始化为什么的任何建议?

Any recommendations as to what this seed value should be initialized to?

推荐答案

好的。因此,首先,我要明确指出, libc 中的PRNG是确定性的(这就是为什么它想要种子)的原因,它使用 LCG -表示一旦有几个值就很容易预测所有值,因此是不安全的。

Alright. So first, let me make it clear that the PRNG in libc is deterministic (that's why it wants a seed), uses an LCG - meaning it's easy to predict all of the values once you have a few, and is therefore insecure.

现在。 erand48()从均匀分布的伪随机实数中返回大小为 double 的随机浮点值。它本身并不需要种子值,而是需要您提供状态缓冲区。完整的声明如下:

Now then. erand48() returns a random floating point value of double size from a uniform distribution of pseudorandom real numbers. It doesn't take a seed value per se, but rather requires you to provide a state buffer. Here's the full declaration:

double erand48(unsigned short xsubi[3]);

状态缓冲区足够有趣,必须以随机值作为种子,以便生成器正常工作。我的第一个念头是从 / dev / urandom 中读取。

The state buffer, amusingly enough, must be seeded by a random value in order for the generator to work. My first thought was to read from /dev/urandom.

我们可以使用类似的方法(使用未缓冲的读取,以防止浪费这么小的读取):

We can do that with something like this (using unbuffered reads to prevent wastage from such small reads):

#include <stdio.h>
#include <stdlib.h>

void *thread_f (void *i) {
    // setup unbuffered urandom
    urandom = fopen ("/dev/urandom", "r");
    setvbuf (urandom, NULL, _IONBF, 0);  // turn off buffering

    // setup state buffer
    unsigned short randstate[3];
    // fgetc() returns a `char`, we need to fill a `short`
    randstate[0] = (fgetc (urandom) << 8) | fgetc (urandom);
    randstate[1] = (fgetc (urandom) << 8) | fgetc (urandom);
    randstate[2] = (fgetc (urandom) << 8) | fgetc (urandom);


    // cleanup urandom
    fclose (urandom);

    // you can now use erand48 (randstate);

    ...     // do whatever work you need to do

    return result;
}

这是线程安全的,甚至可以确保所有种子的种子值都相对安全

This is thread safe, and even ensures a relatively safe seed value for all of the threads.

当然,如果速度不是太大的问题(即:您可以忍受很小的速度损失)并且可以忍受整数,然后直接从 / dev / urandom 进行无缓冲读取是一个完美的解决方案。甚至更好的是,/ dev / urandom提供了安全且不可预测的伪随机整数(从技术上讲,是字节流,但是只要您匹配大小,它们将始终作为整数工作),通常也均匀分布。

Of course, if speed isn't too much of an issue (ie: you can live with a small loss of speed) and you can live with integers, then doing unbuffered reads directly from /dev/urandom is a perfect solution. Even better, /dev/urandom provides secure, unpredictable pseudorandom integers (well, technically, a byte stream, but they will always work as integers as long as you match the size) that are also usually uniformly distributed.

此外, / dev / urandom 会定期注入熵并刷新,以确保您有相当不错的随机数。

Plus, /dev/urandom periodically has entropy injected into it and refreshed, ensuring that you have a decent supply of fairly random numbers.

这篇关于我应该为C中的erand48()使用什么种子值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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