// my_rgamma - generate random variates from the gamma distribution // a demo of the R standalone math library // // Written in 2020 by Charles J. Geyer // // To the extent possible under law, the author(s) have dedicated all // copyright and related and neighboring rights to this software to // the public domain worldwide. This software is distributed without // any warranty. // // See the CC0 Public Domain Dedication // . #define MATHLIB_STANDALONE #include #include #include #include #include // The immediately preceding include and the call to getrandom // to initialize RNG are linux-specific // Must do something else on other OS. int main(int argc, char *argv[]) { if (argc != 4) { fputs("Usage: my_rgamma n alpha beta\n" "where n is a positive integer and alpha and beta" " are postive real numbers\n" "the shape and scale parameters (respectively)\n", stderr); return EXIT_FAILURE; } unsigned int n; double alpha, beta; if (sscanf(argv[1], "%u", &n) != 1) { fputs("failed to read x\n", stderr); return EXIT_FAILURE; } if (sscanf(argv[2], "%lg", &alpha) != 1) { fputs("failed to read alpha\n", stderr); return EXIT_FAILURE; } if (sscanf(argv[3], "%lg", &beta) != 1) { fputs("failed to read beta\n", stderr); return EXIT_FAILURE; } unsigned int seed1, seed2; size_t buflen = sizeof(unsigned int); if (getrandom(&seed1, buflen, GRND_RANDOM) != (ssize_t) buflen || getrandom(&seed2, buflen, GRND_RANDOM) != (ssize_t) buflen) { fputs("failed to initialize random number generator\n", stderr); return EXIT_FAILURE; } set_seed(seed1, seed2); for (unsigned int i = 0; i < n; i++) printf("%12.10f\n", rgamma(alpha, beta)); return EXIT_SUCCESS; }