Problem of the Day
A new programming or logic puzzle every Mon-Fri

Random Number Generator

This one should hopefully having you thinking outside of the box. Without using any of the built in random functions of your chosen programming language or OS, create a function that returns a random number. Feel free to statically define seed numbers if it helps.

Permalink: http://problemotd.com/problem/random-number-generator/

Comments:

  • David - 10 years, 5 months ago

    This generator is simple and has a variable repetition cycle based on the seed, but it has good pseudo-randomness for a small number of values. It was good enough for von Neumann. I modified his algorithm by adding use of current time.

    #include <time.h>
    
    unsigned short int simpleRand(short int k = 0) { //This allows for an optional user seed
     static unsigned long int c = 0;
     unsigned short int x;
     time_t d;
     time(&d);
     if(0 == c) {
         c = (short int) d; 
    }
     c = k;
     //This does nothing for k= 0 c *= c; x = c >> 4 & 0xffff;   return x;
     }
    

    This next function is a bit of being silly.

     int RandAll(int m) { 
    return 221; //Chosen before hand by fair arbitration //Guaranteed to be random
     }
    

    reply permalink

Content curated by @MaxBurstein