- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
#include <stdio.h>
double seed=0.579832467;
main(ac, av)
int ac;
char *av[];
{
/* declare variables */
float *buf, fac;
int sf, ne, i;
/* prototypes? ( shouldn't they be outside the main ) */
double rnd(), sd;
/* gets the number of elements from command line */
ne = atoi(av[1]);
/* assigns the size of float ( in bytes ) to integer value */
sf = sizeof(float);
/* allocates appropriate memory for random number generation */
buf = (float *)malloc(ne*sf);
/* type cast, why?? */
sd = (double)(ne);
/* no idea what initrnd does */
initrnd(sd/(sd+187.9753));
/* checks if memory allocation is successful */
if (buf == NULL)
{
fprintf(stderr, "rndneg: can't allocate %d bytes for buffer\n", ne*sf);
exit(-1);
}
/* fills buffer with random number */
for (i=0; i<ne; i++)
{
buf[i] = (float)(rnd());
}
/* writes the buffer, how does it know the file name? */
write(1, buf, ne*sf);
}
/* random number generating function */
double rnd()
{
seed *= 997.0;
seed -= (double)((int)(seed));
return(seed);
}
initrnd(sd)
/* again no idea, why isn't this function void */
double sd;
{
seed = sd;
return(0);
}
这是 PRNG 的一些代码。我对 C 不是很有经验,这段代码中的一些东西对我来说完全没有意义。我试图对代码发表评论以跟踪发生了什么。如果能澄清一些我不明白的事情,我将不胜感激。特别是同名变量和函数的声明,以及 initrnd 子例程,似乎没有在程序或我在 Internet 上找到的任何库中定义。
非常感谢。
最佳答案
这看起来确实很古老。
对您的问题的一些回答:
initrnd()
只需设置全局 seed
变量为特定值,然后在 PRNG 中使用。stdout
;假定使用文件描述符 1
.这种神奇常数的使用不是很漂亮,应该写成 stdout
(来自 <stdio.h>
)。关于c - 在 C 中无法理解此 PRNG 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6048335/
这个问题是双重的。我正在将 R 脚本翻译成 C++,它使用 L'Ecuyer 组合多重递归生成器 (CMRG) 作为引擎(特别是 MRG32k3a),然后从区间 (0, 1) 上的均匀分布返回一个随机
这个问题是双重的。我正在将 R 脚本翻译成 C++,它使用 L'Ecuyer 组合多重递归生成器 (CMRG) 作为引擎(特别是 MRG32k3a),然后从区间 (0, 1) 上的均匀分布返回一个随机
对于我正在制作的游戏,其中太阳系具有 x 和 y 坐标,我想使用坐标随机生成该太阳系的特征。最简单的方法似乎是用两个种子(x 和 y 坐标)为随机数生成器播种。有没有办法从两颗种子中得到一颗可靠的种子
下面的代码(并且可以按原样编译)导致随机数生成器出于某种原因为所有进程返回完全相同的随机数。怎么可能呢?我对互斥体做错了吗? #include #include #include #includ
我正致力于在 C 中生成非常大的随机整数。我发现/dev/random 是一个很好的来源,但如果熵池耗尽,它就会阻塞。/dev/urandom 似乎是下一个 goto,但随机性的质量相比之下就没那么好
我对函数 rand(x, y, seed) 感兴趣,它根据参数返回(伪)随机数,具有以下属性: 返回的值应该取决于它的 3 个参数,不取决于 rand 到目前为止被调用的次数。例如,假设这些调用按以下
我必须使用这些参数创建一个 PRNG“该程序应使用以下算法生成 1000 个数字的序列:seed = ( 1664525 * seed + 1013904223 ) % 4294967296 对于每个
这个问题的动机是在 Scala 中使用 PRNG,但答案很可能与语言无关。 问题 我想为我的 PRNG 提供一个功能接口(interface)。目前我所知道的 PRNG 实现(Java stdlib、
我正在玩 PRNG(如 Mersenne Twister 和 stdlib 的 rand() 函数),我想要一个好的测试来帮助我确定 PRNG 生成的随机数据的质量。 我使用 PRNG 生成的随机数计
我正在基于AES构建文件加密,必须能够在随机访问模式下工作(访问文件的任何部分)。例如,可以使用Counter中的AES,但众所周知,我们需要一个从未使用过两次的唯一序列。 在这种情况下,可以使用简化
您是否曾经编写过因所使用的(伪)随机数的质量而遇到麻烦的模拟或随机算法? 发生了什么? 您是如何检测/意识到您的 prng 是问题所在的? 切换 PRNG 是否足以解决问题,还是必须切换到真正的随机源
我们有一个脚本可以在我的工作中建立一个新的网络服务器。该脚本涉及创建多个帐户来运行服务、应用程序池等。 我们需要为这些用户中的每一个创建一个密码——即生成一个大约 32 个字符的 ASCII 字符串用
MD5/SHA256/SHA512等可以用作PRNG吗?例如,给定一个整数种子,伪代码是: random_number = truncate_to_desired_range( sha512(
我们有一个脚本可以在我的工作中建立一个新的网络服务器。该脚本涉及创建多个帐户来运行服务、应用程序池等。 我们需要为这些用户中的每一个创建一个密码——即生成一个大约 32 个字符的 ASCII 字符串用
我想知道 Java 中 Math.random() 后面的 PRNG* 的默认种子是什么。据我了解,C 中的时钟是基于系统时钟的。那么Java中是不是也类似呢?另外,每次调用 Math.random(
我正在寻找一种哈希算法,该算法可生成 31/32 位有符号/无符号整数作为 utf8 字符串的摘要,目的是使用输出为 prng 播种,例如 Park-Miller-Carta LCG 或Mersenn
在Java中,我使用“SHA1PNRG”生成伪随机数。我不知道这个数字生成的内部工作原理。什么种子值最适合安全数字生成?我应该使用较小的数字作为种子还是较大的值作为种子?请各位给予正确指导。 我的代码
我想用基于哈希的数据生成器替换现有的基于随机数的数据生成器(Python),这样它就不再需要按顺序生成所有内容,灵感来自 this article . 我可以通过获取哈希的整数版本并将其除以哈希的最大
问题背景 我已经有了针对给定问题的 O(nlog(n)) 解决方案,我将其用作检查器来测试新的更快的解决方案 (O(n))。这两种方法具有相同的签名:它们接收一个数组作为参数并返回一个 long 值。
我想创建一个应用程序,其中登录密码可以作为伪随机数生成器的种子以重新创建加密 key 。然后,该加密 key 将用于加密发送到应用程序数据库和从应用程序数据库发送的所有数据,使用户数据甚至主机都无法访
我是一名优秀的程序员,十分优秀!