Based on your description, your database query is 1's-based. Java nextInt
is 0's-based, that is, .nextInt(10)
will generate a random number between 0 and 9. So your proper solution is to change line 3 to
n = generator.nextInt((int) number) + 1;
this will then give you what you need.
Because at present you are converting both [0] and [1] into 1 in lines 4 & 5, you are skewing your probability distribution. Your first entry is going to get counted twice. If you have an average of 5 entries in your database
, this would give you the 2/5 = 40%
hit rate you're seeing.