I need to get a List of 10 consecutive database records from a random offset in the database each time.
The problem I see with this is overlapping the end boundary.
For example:
Let’s say my database only contains 20 records.
If I choose a random offset between 1 and 20 to start my read of 10 consecutive records and the random number happens to be, say, 15 – then I only get 5 records before I run into the end of the available range.
public getNext10RandomRecords() {
int totalRecs = this.getTotalDatabaseRecordCount();
Random rand = new Random();
// ??
int startOffSet = 15;
int endOffSet = 25;
query.setRange(startOffSet, endOffset); // oops! totalRecs == 20!
}
Choose your random offset to be in the range of
0tototalRecs - 10. That way, the highest start offset will betotalRecs - 10in which case your end offset will be equal tototalRecs.