Interview Question
Software Engineer / Developersstore random numbers in a table :
Difference generator :
ran[1...100];
i=100;
while( i++<n )
{
ran[i]=( ran[i-37] - ran[i-99] )& ( 1<<32 - 1) ;
}
Here is the code to produce a random number (less than 65535) once every second.
#include <iostream>
#include <time.h>
#include <windows.h>
using namespace std;
#define HASHSIZE 65535
unsigned hash(char *s)
{
unsigned hashval;
for (hashval = 0; *s != '\0'; s++)
hashval = *s + 31 * hashval;
return hashval % HASHSIZE;
}
void main()
{
while(1)
{
char *date;
time_t timer;
timer=time(NULL);
date = asctime(localtime(&timer));
printf("%d ", hash(date));
Sleep(1000);
}
}
- JD February 13, 2010