随机数发生器。 [英] Random number generator.

查看:73
本文介绍了随机数发生器。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个随机数生成器实现与

以下要求:


- 线程安全,可重入。

- 在给定种子的情况下生成一致可重复的伪随机

数字序列。

- 相对均匀,不必完美。


该应用程序不是安全或统计应用程序,

数字质量不是优先考虑的事情,尽管相当统一的

分布会很好。我使用它的应用程序是

生成用于控制音频和视频效果的随机数,并且

用户必须能够指定种子来生成相同的序列

的随机每次都是数字。然而,可能有许多这样的b&b流。同时生成的随机数,每个

用它自己的起始值播种,并且必须产生独立于其他所有流的序列。 。


这是我想要添加到我的应用程序的一个次要功能(现在只使用rand()而没有可预测性的
)。因此,说实话,我对做任何大量工作或

研究都不感兴趣。我想知道是否有人知道一个不错的实现

很容易投入到现有代码中(在STL中似乎没有任何东西是
,是那里?)。


谢谢,

杰森

解决方案

2008年-05-31 19:09, ja ************ @ gmail。 com 写道:


我正在寻找一个随机数生成器实现,并带有

以下要求:


- 线程安全,可重入。

- 在给定种子的情况下,生成一致可重复的伪随机

数字序列。

- 相对统一,不一定是完美的。


该应用程序不是安全或统计应用程序,

质量数字是虽然相当统一的b $ b分配会很好,但不是优先考虑的问题。我使用它的应用程序是

生成用于控制音频和视频效果的随机数,并且

用户必须能够指定种子来生成相同的序列

的随机每次都是数字。然而,可能有许多这样的b&b流。同时生成的随机数,每个

用它自己的起始值播种,并且必须产生独立于其他所有流的序列。 。


这是我想要添加到我的应用程序的一个次要功能(现在只使用rand()而没有可预测性的
)。因此,说实话,我对做任何大量工作或

研究都不感兴趣。我想知道是否有人知道一个不错的实现

很容易投入到现有代码中(在STL中似乎没有任何东西是
,是那里?)。



我不知道均匀性,但是使用srand()并用最后生成的数字(其函数)调用它b / b
)应该成功

重新进入。


-

Erik Wikstr ?? m


5月31日下午1:30,Erik Wikstr?m< Erik-wikst ... @ telia.comwrote:


我不知道统一性,但是使用srand()并将其称为

,最后生成的数字(其函数)应该是

重入。



谢谢,我想这是一个非常明显的解决方案。所以类似

这个:


class RandomNumber {

public:

RandomNumber(int seed) ):seed_(种子){}

void Seed(int seed){seed_ = seed; }

int Next(){srand(seed_); return rand(); }

私人:

int seed_;

};


我的问题是,是rand()保证给每个平台和每台机器上的种子提供相同的序列

,对于每个

标准库实现?我的特定应用程序确实运行在许多平台和许多计算机上,并且用户希望在给定相同输入的每台机器上看到

完全相同的结果。


谢谢,

Jason


5月31日下午1:37,jason.cipri。 .. @ gmail.com"

< jason.cipri ... @ gmail.comwrote:


class RandomNumber {

public:

RandomNumber(int seed):seed_(seed){}

void Seed(int seed){seed_ = seed; }

int Next(){srand(seed_); return rand(); }

私人:

int seed_;

};



哎呀。那当然应该是:


int Next(){

srand(seed_);

seed_ = rand( ); //< ----

返回seed_;

}


I am looking for a random number generator implementation with the
following requirements:

- Thread-safe, re-entrant.
- Produces consistently reproducible sequences of psuedo-random
numbers given a seed.
- Relatively uniform, does not have to be perfect.

The application is not a security or statistics application, the
quality of numbers is not a priority although a fairly uniform
distribution would be nice. The application I am using it for is
generating random numbers for controlling audio and video effects, and
the user must be able to specify a seed to produce the same sequence
of "random" numbers every time. However, there may be many such
"streams" of random numbers being generated at the same time, each
which is seeded with it''s own starting value and must produce
sequences independent of every other "stream".

This is a minor feature I want to add to my application (which is just
using rand() with no predictability right now). Therefore, to be
honest, I am not interested in doing any major amount of work or
research. I am wondering if anybody knows of a decent implementation
that is easy to drop in to existing code (there doesn''t appear to be
anything in the STL, is there?).

Thanks,
Jason

解决方案

On 2008-05-31 19:09, ja************@gmail.com wrote:

I am looking for a random number generator implementation with the
following requirements:

- Thread-safe, re-entrant.
- Produces consistently reproducible sequences of psuedo-random
numbers given a seed.
- Relatively uniform, does not have to be perfect.

The application is not a security or statistics application, the
quality of numbers is not a priority although a fairly uniform
distribution would be nice. The application I am using it for is
generating random numbers for controlling audio and video effects, and
the user must be able to specify a seed to produce the same sequence
of "random" numbers every time. However, there may be many such
"streams" of random numbers being generated at the same time, each
which is seeded with it''s own starting value and must produce
sequences independent of every other "stream".

This is a minor feature I want to add to my application (which is just
using rand() with no predictability right now). Therefore, to be
honest, I am not interested in doing any major amount of work or
research. I am wondering if anybody knows of a decent implementation
that is easy to drop in to existing code (there doesn''t appear to be
anything in the STL, is there?).

I do not know about the uniformity, but using srand() and calling it
with the last generated number (of a function thereof) should make it
re-entrant.

--
Erik Wikstr??m


On May 31, 1:30 pm, Erik Wikstr?m <Erik-wikst...@telia.comwrote:

I do not know about the uniformity, but using srand() and calling it
with the last generated number (of a function thereof) should make it
re-entrant.

Thanks, I guess that is a pretty obvious solution. So something like
this:

class RandomNumber {
public:
RandomNumber (int seed) : seed_(seed) { }
void Seed (int seed) { seed_ = seed; }
int Next () { srand(seed_); return rand(); }
private:
int seed_;
};

My question there is, is rand() guaranteed to give the same sequence
of numbers given a seed on every platform and every machine, for every
standard library implementation? My particular application does run on
many platforms and many computers, and the user will expect to see the
exact same results on every machine given the same inputs.

Thanks,
Jason


On May 31, 1:37 pm, "jason.cipri...@gmail.com"
<jason.cipri...@gmail.comwrote:

class RandomNumber {
public:
RandomNumber (int seed) : seed_(seed) { }
void Seed (int seed) { seed_ = seed; }
int Next () { srand(seed_); return rand(); }
private:
int seed_;
};

Oops. That should, of course, be:

int Next () {
srand(seed_);
seed_ = rand(); // <----
return seed_;
}


这篇关于随机数发生器。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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