Google Interview Question
Software Engineer in TestsCountry: United States
Interview Type: In-Person
take 100000 entries
assume that the range in which the random number are being generated is between minimum and maximum (can be assumed for large entries)
Now, for each position, count the number of times the next number appearing after it is more than it or not. Take this ratio. This ratio should be proportioinal to the (Max - E)/(Max-Min)
where E is the current value
Now, plotting these values for all positions, we take the variance. If the variance is small, then the random number generator can be assumed good
Hi ,
What if we use time to generate the randon number. Get the current time in milliseconds and divide it by 1000.
class TimeTest{
public static void main(Strings[] args){
system.out.println(System.currentTimeMilliseconds()/1000);
}
}
This should generate unique number definetly.
Regards,
Vijay Kalkundri
Generate random numbers in large range and plot the random numbers in a graph which should be a uniform distribution for all the numbers in the range.
1.) we can check how the elements distribute over a given range
2.) we can check how many times one number is hit and what is the difference between the most hit number and the least hit one.
3.) *Visually check for patterns of each number hits by looking (in the code bellow I would look) the "map".
point 1.) sample Java code:
public static double randPercent() {
int N = 1000;
int[] map = new int[N];
int cnt = 0;
int rand;
for (int i = 0; i < N; i++) {
rand = (int) (Math.random() * N);
map[rand]++;
if (map[rand] == 0)
cnt++;
}
//System.out.format("Result is %s (different numbers) out of %s", cnt, N);
return (double) cnt / N;
}
This is just one of the many tests you need to do. See Martin's response for one (out of many possible) examples of why your approach is woefully insufficient.
Some additional tests would be making sure there's no correlation between one number and the next, checking longest runs of ones and zeros, checking criteria like dimensional equi-distribution, and a zillion and one other tests. You can read about all of these on the Internet. This is a very nontrivial problem.
None of the solution above really works.
Think about this fake random number generator.
static n = 0;
void fakerand()
{
n++;
if(n%2==0)
return rand()*0.5;
else
return 1-(rand()*0.5);
}
We need to generate a really long sequence. And try to proof, as much as we can, that there is no pattern in this sequence.
Chi Square test, distribution test
- Ashupriya August 13, 2012