/*
  randu.c - a bad implementation of a bad algorithm 
  see http://en.wikipedia.org/wiki/RANDU
  scruss - 2013-06-02
  compiles rather nicely with:
   gcc -std=c99 -pedantic -O2 -Wall -o randu randu.c
*/
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>

static uint32_t randu(uint32_t a)
{
    return ((uint64_t) a * 65539UL) % 2147483648UL;
}

int main(int argc, char *argv[])
{
    uint32_t a;
    unsigned int seed;
    if (argc != 2) {
	fprintf(stderr, "usage: %s seed\n", argv[0]);
	exit(EXIT_FAILURE);
    }
    else {
	if (sscanf(argv[1], "%ui", &seed) != 1) {
	    (void) fputs("error - seed must be an integer\n", stderr);
	    exit(EXIT_FAILURE);
	}
	/* initial seed must be odd */
	if (seed % 2 == 0) {
	    seed--;
	}
	a = randu((uint32_t) seed);
	for (;;) {
	    (void) fwrite(&a, sizeof(a), (size_t) 1, stdout);
	    a = randu(a);
	}
    }
    /* will never be reached */
}
