且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

"帽"容器类[C ++]

更新时间:2023-02-15 20:57:19

AngleWyrm写道:
AngleWyrm wrote:
我创建了一个新的容器类,称为帽子。

它提供随机选择的用户对象,无论是否有
替换,概率不均匀。统一概率是一种退化的情况,也可以存储。

这是网页,有完整的来源,插图和示例
用法。看一看,然后发表评论吧!

http://home.comcast.net/~anglewyrm/hat.html
I have created a new container class, called the hat.

It provides random selection of user objects, both with and without
replacement, with non-uniform probabilities. Uniform probabilities are a
degenerate case, and can also be stored.

Here''s the web-page, with complete source, illustration, and example
usage. Take a look-see, and post your critique!

http://home.comcast.net/~anglewyrm/hat.html



我只是简单地看一下我的宠物主题是什么:随机的

数字生成器:


模板< class T>

int hat< T> :: random(int range )

{

//确保在使用rand之前调用srand

if(!rand_seeded)

{

srand(time(NULL));

rand();

rand_seeded = true;

}

int random_number = rand()%range;


返回random_number;

};

我会选择一个随机数生成器给客户端,比如

它是在std :: random_shuffle中完成的。可以使用第二个模板参数

。您还可以证明一个构造函数将一个

RNG的实例作为参数。您必须提供合理的默认值,即

课程。没有为用户做出这种选择是有充分理由的。对于

例如:


a)那里有一个*巨大的*各种随机数发生器。对于一个图书馆设计师来说,对于给定的客户问题,这些将是好的

是不可能预测的。


b)如果用户可以拥有

随机数生成器的独立实例,这是一个很大的优势。

现在为您选择的那个:依赖于std :: rand()来自< cstdlib>

让您受操作系统的支配。您的代码假定rand()的低位比特分配得相当好。请注意,

一些随机数生成器(例如直接线性同余

RNG)在低阶字节方面存在一些问题。在一些运行

系统的系统中,这些系统可能仍在使用中。因此,优良作法是使用

高阶字节而不是如此(引自手册页):


C中的数字食谱:艺术科学计算

(William H. Press,Brian P. Flannery,Saul A. Teukolsky,

William T. Vetterling;纽约:剑桥大学

Press,1992(第2版,第277页)),以下评论是



"如果你想生成一个随机整数1

和10,你应该总是使用高阶

位,如


j = 1 +( int)(10.0 * rand()/(RAND_MAX + 1.0));


从不通过类似的东西


j = 1 +(rand ()%10);


(使用低位位)。


现在,在Linux上,这个建议有点过时。而且希望弱的RNG最终会消亡。但是,我建议在第一个

的地方提供一个

合理的默认随机数生成器,以确定低阶位的质量。

BTW,干得好。

***


Kai-Uwe


I only had a very brief look at what is a pet subject of mine: the random
number generator:

template <class T>
int hat<T>::random(int range)
{
// make sure srand has been called before using rand
if( !rand_seeded )
{
srand( time(NULL) );
rand();
rand_seeded = true;
}
int random_number = rand() % range;

return random_number;
};
I would leave the choice of a random number generator to the client, like
it is done in std::random_shuffle. A second template parameter can be used
for that. You could also prove a constructor that takes an instance of an
RNG as an argument. You would have to provide a reasonable default, of
course. There are good reasons for not making this choice for the user. For
example:

a) There is a *huge* variety of random number generators out there. It is
impossible to predict for a library designer which of these will be good
for a given client problem.

b) It is a big advantage if the user can have independent instances of
random number generators.
Now for the one that you have chosen: relying on std::rand() from <cstdlib>
puts you at the mercy of the operating system. Your code assumes that the
low order bits of rand() are reasonably well distributed. Please note that
some random number generators (e.g. straight forward linear congruence
RNGs) have some troubles with the low order bytes. On some operating
systems those might still be in use. Thus it is good practice to use the
high order bytes instead like so (quoted from the man page):

In Numerical Recipes in C: The Art of Scientific Computing
(William H. Press, Brian P. Flannery, Saul A. Teukolsky,
William T. Vetterling; New York: Cambridge University
Press, 1992 (2nd ed., p. 277)), the following comments are
made:
"If you want to generate a random integer between 1
and 10, you should always do it by using high-order
bits, as in

j=1+(int) (10.0*rand()/(RAND_MAX+1.0));

and never by anything resembling

j=1+(rand() % 10);

(which uses lower-order bits)."

Now, on Linux, this advice is somewhat outdated. And it is to be hoped that
weak RNGs will eventually die out. However, I would reccommend to provide a
reasonable default random number generator of your choosind in the first
place for which the quality of low order bits is established.
BTW, nice job.
Best

Kai-Uwe


Kai -Uwe Bux写道:
Kai-Uwe Bux wrote:
AngleWyrm写道:
AngleWyrm wrote:
我创建了一个新的容器类,称为帽子。


这是网页,有完整的来源,插图和示例
用法。看一看,然后发表评论吧!

http://home.comcast.net/~anglewyrm/hat.html

我只是简单地看一下我的宠物主题是什么:随机的数字生成器:

模板< class T>
int hat< T> :: random(int range)
{
//确保srand有在使用rand之前被调用
if(!rand_seeded)
{/ / srand(time(NULL));
rand();
rand_seeded = true;
}
int random_number = rand()%range;

return random_number;
};

我会选择随机数生成器对于客户端,例如
它是在std :: random_shuffle中完成的。可以使用第二个模板参数
。您还可以证明一个构造函数,它将一个
RNG的实例作为参数。你必须提供一个合理的默认值,当然。没有为用户做出这样的选择是有充分理由的。
例如:

a)有一个*巨大的*各种随机数生成器。对于图书馆设计师来说,无法预测哪一个对于给定的客户问题会是好的。

b)如果用户可以独立,这是一个很大的优势
随机数生成器的实例。

现在为你选择的那个:依赖于
< cstdlib>的std :: rand()让你受操作系统的支配。你的代码假设
I have created a new container class, called the hat.

It provides random selection of user objects, both with and without
replacement, with non-uniform probabilities. Uniform probabilities are a
degenerate case, and can also be stored.

Here''s the web-page, with complete source, illustration, and example
usage. Take a look-see, and post your critique!

http://home.comcast.net/~anglewyrm/hat.html

I only had a very brief look at what is a pet subject of mine: the random
number generator:

template <class T>
int hat<T>::random(int range)
{
// make sure srand has been called before using rand
if( !rand_seeded )
{
srand( time(NULL) );
rand();
rand_seeded = true;
}
int random_number = rand() % range;

return random_number;
};
I would leave the choice of a random number generator to the client, like
it is done in std::random_shuffle. A second template parameter can be used
for that. You could also prove a constructor that takes an instance of an
RNG as an argument. You would have to provide a reasonable default, of
course. There are good reasons for not making this choice for the user.
For example:

a) There is a *huge* variety of random number generators out there. It is
impossible to predict for a library designer which of these will be good
for a given client problem.

b) It is a big advantage if the user can have independent instances of
random number generators.
Now for the one that you have chosen: relying on std::rand() from
<cstdlib> puts you at the mercy of the operating system. Your code assumes



^^^^^^^^^^^^^^^^^

哎呀:它当然是你的供应商cstdlib。仍然存在风险!

rand()的低阶位分配得相当好。请注意,一些随机数发生器(例如直线性线性同余RNG)在低阶字节方面存在一些问题。在某些操作系统中,那些可能仍在使用中。因此,优良作法是使用高阶字节而不是如此(引自手册页):

C中的数字食谱:科学计算的艺术
( William H. Press,Brian P. Flannery,Saul A. Teukolsky,
William T. Vetterling;纽约:剑桥大学
出版社,1992(第2版,第277页)),以下评论如果要生成1
到10之间的随机整数,则应始终使用高位
位,如

j = 1 +(int)(10.0 * rand()/(RAND_MAX + 1.0));

从不通过任何类似的东西

j = 1+(rand()%10);

(使用低阶位)。现在,在Linux上,这个建议有点过时了。并且希望
^^^^^^^^^^^^^


相同的oops。

弱RNG将会最终消亡。但是,我建议
首先为你的choosind提供一个合理的默认随机数生成器,这是第一个建立低阶位质量的地方。

BTW,不错工作。

***

Kai-Uwe


^^^^^^^^^^^^^^^^^
Oops: it is of course the vendor of your cstdlib. Still its a risk!
that the low order bits of rand() are reasonably well distributed. Please
note that some random number generators (e.g. straight forward linear
congruence RNGs) have some troubles with the low order bytes. On some
operating systems those might still be in use. Thus it is good practice to
use the high order bytes instead like so (quoted from the man page):

In Numerical Recipes in C: The Art of Scientific Computing
(William H. Press, Brian P. Flannery, Saul A. Teukolsky,
William T. Vetterling; New York: Cambridge University
Press, 1992 (2nd ed., p. 277)), the following comments are
made:
"If you want to generate a random integer between 1
and 10, you should always do it by using high-order
bits, as in

j=1+(int) (10.0*rand()/(RAND_MAX+1.0));

and never by anything resembling

j=1+(rand() % 10);

(which uses lower-order bits)."

Now, on Linux, this advice is somewhat outdated. And it is to be hoped ^^^^^^^^^^^^^

Same oops.
that weak RNGs will eventually die out. However, I would reccommend to
provide a reasonable default random number generator of your choosind in
the first place for which the quality of low order bits is established.
BTW, nice job.
Best

Kai-Uwe






" Kai-Uwe BUX&QUOT; &LT; JK ******** @ gmx.net&GT;在消息中写道

news:ca ********** @ news01.cit.cornell.edu ...
"Kai-Uwe Bux" <jk********@gmx.net> wrote in message
news:ca**********@news01.cit.cornell.edu...
RNGs)有一些麻烦低位字节。在某些操作系统中,那些可能仍在使用中。因此,优良作法是使用
高阶字节而不是如此(引自手册页):

C中的数字食谱:科学计算的艺术
( William H. Press,Brian P. Flannery,Saul A. Teukolsky,
William T. Vetterling;纽约:剑桥大学
出版社,1992(第2版,第277页)),以下评论如果要生成1
到10之间的随机整数,则应始终使用高位
位,如

j = 1 +(int)(10.0 * rand()/(RAND_MAX + 1.0));

从不通过任何类似的东西

j = 1+(rand()%10);

(使用低阶位)。现在,在Linux上,这个建议有点过时了。希望
RNGs) have some troubles with the low order bytes. On some operating
systems those might still be in use. Thus it is good practice to use the
high order bytes instead like so (quoted from the man page):

In Numerical Recipes in C: The Art of Scientific Computing
(William H. Press, Brian P. Flannery, Saul A. Teukolsky,
William T. Vetterling; New York: Cambridge University
Press, 1992 (2nd ed., p. 277)), the following comments are
made:
"If you want to generate a random integer between 1
and 10, you should always do it by using high-order
bits, as in

j=1+(int) (10.0*rand()/(RAND_MAX+1.0));

and never by anything resembling

j=1+(rand() % 10);

(which uses lower-order bits)."

Now, on Linux, this advice is somewhat outdated. And it is to be hoped






感谢您的投入,我现在正致力于模板化的方式

用户指定的函数,它有一个默认值。不幸的是

random_shuffle使用了一种相当粗略的方法:它只是定义了两个

函数,一个有一个,一个没有。对我来说,这似乎是非C ++,我不能让自己重新编写标题中的副本。必须有更好的方式来获得
。还在研究那个。


关于低阶位的建议......我做了一些

研究(以执行代码的形式,并观察结果)和

因为事实证明std :: rand()的第一个返回值是一个序列号

三个步骤通过RAND_MAX。我的版本无论如何。


我得到的是,如果srand(时间(NULL))用作种子,那么它将花费大约9个小时
循环通过范围。


that

Thanks for the input, I''m now working on a way to template-ize a
user-assigned function, which would have a default. Unfortunately
random_shuffle uses a rather coarse method: It simply defines two
functions, one with and one without. This seems so un-C++ to me that I
can''t bring myself to recode a duplicate in the header. There''s got to be a
better way. Still researching on that one.

As to the advice that was given about low-order bits...I''ve done some
research on this (in the form of executing code, and observing results) and
as it turns out std::rand()''s first return value is a sequential number
that steps by threes through RAND_MAX. My version anyway.

What I got was that if srand( time(NULL) ) is used as a seed, then it will
take about nine hours to cycle through the range.