int totalRecs = getTotalRecs(); // returns '13'
int MAX_RECS_PER_LOOKUP = 10;
Random rand = new Random();
int start = (totalRecs > Utl.MAX_RECS_PER_LOOKUP) ? rand.nextInt(totalRecs-Utl.MAX_RECS_PER_LOOKUP) : 0;
results = this.getNewRecs(filter, start, start+1);
I’m using this random number logic to retrieve records from the database based on their table row offset (start up to start+1).
But for some reason it keeps repeating the same numbers: start value of 0 and start value of 3.
I tried seeding the Random object but it made no difference:
long seed = System.currentTimeMillis();
rand.setSeed(seed);
The
rand.nextInt(n)method returns a value between 0 (inclusive) and n (exclusive).In your code, if
totalRecsis 13, andUtl.MAX_RECS_PER_LOOKUPis 10 (I am assuming this one based on theMAX_RECS_PER_LOOKUPvalue in your code snippet, but I may be wrong), then this code:should return a random number between 0 and 2.
I am not sure if this is what you actually want?