#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);
}
This is some code for a PRNG. I am not very experienced with C and some of the things in this code make absolutely no sense to me. I tried to comment to code to track what's going on. I would appreciate it if some of the things I don't understand could be cleared up. Especially the declarations of variables and functions with the same name, as well as the initrnd subroutine, that doesn't seem to be defined in the program or any library I could find on the internet.
Thanks a lot.
This looks positively ancient.
A few answers to your questions:
initrnd()
just sets the global seed
variable to a specific value, that is then used in the PRNG.stdout
; which is assumed to be using file descriptor 1
. This use of a magical constant is not very pretty, it should be written as stdout
(from <stdio.h>
).