问题描述
你好。
我想生成具有给定概率的随机数,比如说80%
甚至20%奇数。可以在C中实现这样的算法吗?
是的,如果你可以从某个地方获得令人满意的随机位。 (如果你是
不过分挑剔,rand()会提供给他们。)
-
Richard Heathfield
Usenet是一个奇怪的地方 - dmr 29/7/1999
电子邮件:rjh在上述域名中, - www。
不在标准C中,没有。为了生成随机数,你必须有一个非确定性信息来源 - 这是随机的
。 C本身不提供任何真正的随机操作,
和诸如获取一天中的时间或定时循环的黑客往往不会非常随机。
C提供了一些伪随机函数,
的实际随机性随实现而变化很大。 comp.lang.c常见问题
可能描述了几种可能性,例如
Mersenne Twister()
的实现相当容易。
-
有什么可说的,看,这是新的吗?它已经过时了,这已经是我们面前的了。 - 传道书
这取决于你的意思随机。
如果你的意思是随机从某种意义上说算法通过某些随机性的统计测试,那么可能有一两个内置在
C库中,但你也可以自己滚动(看到最后的URL)。
至少有一些基于素数模型,椭圆曲线等的算法。
这些算法的输出适用于模拟和类似的
活动中的对手不参与。我知道的每个随机数
期间至少是2 ^ 31左右,但我确定那些期间有更大的
期。 (像这样的算法实际上是伪随机的,而不是随机的。)
如果你的意思是随机从某种意义上说,攻击者无法猜出下一个数字将是什么......那么你必须要小心谨慎。 (事实上,
RSA的Securid产品系列旨在使这一点变得非常困难。)如果你是处理加密的
...的概念统计随机性是否与攻击者是否可以引出算法
和/或种子的概念无关......算法可以通过统计测试而不是
适用于其目标是防止猜测下一个数字。 (事实上,你可能想要查看一次性密码的概念......这些最好是基于物理过程的
,例如投掷硬币。 )
有用的网址:
Hi there.
I want to generate random numbers with a given probability, say 80%
even and 20% odd. Is it possible to implement such an algorithm in C?
Yes, if you can get satisfactorily random bits from somewhere. (If you''re
not overly fussy, rand() will supply them.)
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Not in standard C, no. In order to generate random numbers, you
have to have a source of non-deterministic information -- something
that is random. C itself does not provide any true random operations,
and hacks such as getting the time of day or timing a loop tend not to
really be very random at all.
C provides some pseudo-random functions, the actual randomness of
which varies considerably with the implementation. The comp.lang.c FAQ
probably describes several possibilities, such as the
Mersenne Twister ( http://en.wikipedia.org/wiki/Mersenne_twister )
implementations of which are fairly easily available.
--
Is there any thing whereof it may be said, See, this is new? It hath
been already of old time, which was before us. -- Ecclesiastes
It depends on what you mean by "random".
If you mean "random" in the sense that the algorithm passes certain
statistical tests of randomness, then there may be one or two built into the
C library, but also you can roll your own (see the URLs at the end). There
are at least a few algorithms based on prime moduli, eliptic curves, etc.
The output of these algorithms would be suitable for simulations and similar
activities where an "opponent" is not involved. Every random number
algorithm that I''m aware of has (a)a "seed" or "internal state", and (b)a
finite period in which the generated sequence will repeat. The typical
period is at least 2^31 or so, but I''m sure there are those with much larger
periods. (Algorithms like this are really pseudo-random, not random.)
If you mean "random" in the sense that an attacker cannot guess what the
next number will be ... then you have to be far more careful. (In fact,
RSA''s Securid product line is designed to make this very hard.) If you''re
dealing with cryptography ... the notion of statistical randomness is
unrelated to the notion of whether an attacker can elicit the algorithm
and/or the seed ... an algorithm can pass statistical tests but not be
suitable if its goal is to prevent guessing the next number. (In fact, you
might want to review the notion of the one-time cryptopad ... these are best
based on physical processes, such as tossing a coin.)
Helpful URLs:
http://en.wikipedia.org/wiki/Pseudor...mber_generator
http://en.wikipedia.org/wiki/List_of...ber_generators
http://www.rsasecurity.com/node.asp?id=3050
http://www.random.org/
http://random.mat.sbg.ac.at/generators/
http://random.mat.sbg.ac.at/links/rando.html
http://www.agner.org/random/
这篇关于随机数生成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!