#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 上找到的任何库中定义。
非常感谢。