/* Some simple C code for a pseudorandom number generator which uses the mod function, in a fashion slightly different than the one used in class. The numbers used were carefully chosen to provide a sufficiently long repeating sequence of numbers for any initial seed value. The book "Numerical Recipes in C" has some useful tips on random number production, including algorithms much better than this one, which is based on code in CMU's AI repository (http://www.cs.cmu.edu/Web/Groups/AI/html/air.html) */ #include int _seed_; /* this function initializes the random number seed */ void randomize(int n) { _seed_ = n; } int randint() { _seed_=((_seed_%127773)*16807)-((_seed_/127773)*2836); _seed_+=(_seed_?0:2147483647); return _seed_; } /* just produce a handful of "random" numbers */ main() { int i; randomize(time(0)); for (i=0;i<20;i++) printf("%d\n",randint()); }