File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Selecting random records Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Selecting random records" Watch "Selecting random records" New topic

Selecting random records

Sri Rangan
Ranch Hand

Joined: Dec 08, 2001
Posts: 160
After I retrieve the records from database by some search criteria, I need to randomly pickup particular number of records based on the user input.
Assuming database resultset retrieves 1000 records and I have pick up 350 random from them
Is there a good way to approach this.
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Have a look at the ResultSet.absolute(int rowNumber) method; I think that would solve your problem.
kind regards
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
You would want to generate the 350 random numbers into an array, sort them, and then either use the absolute() method or simply iterate through the ResultSet only keeping the records that match an entry in your array. Bouncing around back and forth through the ResultSet would probably be a performance pig.

Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
I agree. Here's the link:
subject: Selecting random records
It's not a secret anymore!