计算标准C中随机素数的方法。 [英] Way for computing random primes in standard C.

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

问题描述

大家好,


标准C库中是否有一个函数返回一个主要的

数字,它也是伪随机的?


假设没有,正如我所拥有的文档中所显示的那样,有更好的方式比填充范围0的数组更好... RAND_MAX使用

预先计算的素数并使用rand()的输出将其索引到

提取随机素数。


使用srand(1)重新初始化rand()函数是什么意思?

是否意味着对rand()的进一步调用将返回带有新的
的数字初始点?如果是这样,使用

种子值调用srand()有什么不同,例如time()函数返回的值?


谢谢大家寻求帮助。我发现这个小组非常有用。

解决方案

在文章< dt ********** @ emma.aioe.org> ,

fieldfallow< fi ********* @ gmail.com>写道:

标准C库中是否有一个函数返回一个也是伪随机的素数?


No.


假设没有,正如我所拥有的文档中所显示的那样,是否存在
比使用预先计算的素数填充范围0 ... RAND_MAX的数组更好的方法,并使用rand()的输出来索引它以提取随机素数。


''更好''取决于对阵列的访问密度。

如果你要进行大量访问,那么预先计算<根据筛子或类似技术,
可能是最好的。但是,如果

访问非常稀疏,那么使用

中的一个来计算N''素数的公式可能会更好,并从

估计值向前,直到你找到一个实际的素数 - 有

众所周知的素性测试可以比

做得快得多顺序分解尝试。


使用srand(1)重新初始化rand()函数是什么意思?
是否意味着对rand()的进一步调用将返回数字有一个新的
起点?


起点将被重置为任何矢量

由种子1决定。

如果是这样,怎么样它与使用诸如time()函数返回的种子值调用srand()不同吗?




如果你用time()播种那么为了重复序列

你需要知道播种时的时间()是什么。

如果你用1等常数种子,那么总是遵循相同的路径

- 如果你在同一个过程中用1重新设置,

那么它将返回并开始重用完全相同的<再次b / b


-

没有人有权通过
$ b来摧毁另一个人的信念$ b要求经验证据。 - Ann Landers


Walter Roberson写道:


文章< dt ********** @ emma.aioe.org>,
fieldfallow< fi ********* @ gmail.com>写道:

标准C库中是否有一个函数返回一个也是伪随机的素数?



否/ blockquote>


如何在数组中生成N个素数列表,然后使用

a PRNG来选择该数组中的下标?


[...]


-

+ ------------- ------------ + -------------------- + ---------------- ------------- +

| Kenneth J. Brody | www.hvcomputer.com | |

| kenbrody / at\spamcop.net | www.fptech.com | #include< std_disclaimer.h> |

+ ------------------------- + -------------- ------ + ----------------------------- +

不要邮件给我:< mailto:Th ************* @ gmail.com>


fieldfallow写道:

标准C库中是否有一个函数返回一个也是伪随机的素数?

假设没有,因为它出现在文档中我有,有一个更好的方法比填充范围0 ... RAND_MAX与
预先计算的素数并使用rand()的输出索引到
提取随机素数。




我可能会尝试这样的事情


#include< stdlib.h>


int is_prime(int);

/ *实现为了简洁而遗漏:) * /


int random_prime( ){

int candidate = 0;


while(!is_prime(candidate)){

candidate = rand();

}

返回候选人;

}

如果事实它很慢(可能甚至*非常*慢)而且只返回

primes up到RAND_MAX补偿了对阵列使用的RAND_MAX * sizeof(prime)

字节内存的需求。


-

如果您通过Google发布,请阅读< http://cfaj.freeshell.org/google>


Hello all,

Is there a function in the standard C library which returns a prime
number which is also pseudo-random?

Assuming there isn''t, as it appears from the docs that I have, is there
a better way than to fill an array of range 0... RAND_MAX with
pre-computed primes and using the output of rand() to index into it to
extract a random prime.

Also what is meant by reinitialising the rand() function with srand(1)?
Does it mean that further calls to rand() will return numbers with a new
starting point? If so, how is it different to calling srand() with a
seed value such as that returned by the time() function?

Thank you all for the help. I find this group very useful.

解决方案

In article <dt**********@emma.aioe.org>,
fieldfallow <fi*********@gmail.com> wrote:

Is there a function in the standard C library which returns a prime
number which is also pseudo-random?
No.

Assuming there isn''t, as it appears from the docs that I have, is there
a better way than to fill an array of range 0... RAND_MAX with
pre-computed primes and using the output of rand() to index into it to
extract a random prime.
''better'' would depend upon the density of access to the array.
If you are going to have lots of accesses then pre-computing
according to sieve or similar techniques might be best. If, though,
access is quite sparse, then it might be better to use one of
the formulas for estimating the N''th prime, and search from the
estimated value forward until you find an actual prime -- there are
well-known primality tests that can be much much faster than
doing a sequential factorization attempt.

Also what is meant by reinitialising the rand() function with srand(1)?
Does it mean that further calls to rand() will return numbers with a new
starting point?
The starting point would have been reset to whatever vector is
conditioned by the seed 1.
If so, how is it different to calling srand() with a
seed value such as that returned by the time() function?



If you seed with time() then in order to repeat the sequence
you need to know what the time() was at the time of the seeding.
If you seed with a constant such as 1, then the same path will
always be followed -- and if you reseed with 1 in the same process,
then it will go back and start reusing the exact same sequence of
numbers again.
--
"No one has the right to destroy another person''s belief by
demanding empirical evidence." -- Ann Landers


Walter Roberson wrote:


In article <dt**********@emma.aioe.org>,
fieldfallow <fi*********@gmail.com> wrote:

Is there a function in the standard C library which returns a prime
number which is also pseudo-random?



No.



What about generating a list of N primes into an array, and then using
a PRNG to select a subscript into that array?

[...]

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don''t e-mail me at: <mailto:Th*************@gmail.com>


fieldfallow wrote:

Is there a function in the standard C library which returns a prime
number which is also pseudo-random?

Assuming there isn''t, as it appears from the docs that I have, is there
a better way than to fill an array of range 0... RAND_MAX with
pre-computed primes and using the output of rand() to index into it to
extract a random prime.



I might try something like this

#include <stdlib.h>

int is_prime(int);
/* implementation left out for brevity :) */

int random_prime() {
int candidate = 0;

while (!is_prime(candidate)) {
candidate = rand();
}
return candidate;
}
if the fact that it is slow (probably even *very* slow) and only returns
primes up to RAND_MAX compensates the need for RAND_MAX * sizeof(prime)
bytes of memory used for the array.

--
If you''re posting through Google read <http://cfaj.freeshell.org/google>


这篇关于计算标准C中随机素数的方法。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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