#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サブルーチンは、インターネット上で見つけたプログラムやライブラリでは定義されていないようです。
どうもありがとう。