Solved

Best way to pick a random subset from a collection

Posted on 2013-12-07
6
390 Views
Last Modified: 2013-12-07
I have a large ArrayList and I would like to generate a random subset list of four, without repeats.  I was wondering what the best way to do this would be in Java

Thanks in advance!
0
Comment
Question by:kpbarem
6 Comments
 
LVL 16

Expert Comment

by:krakatoa
Comment Utility
Out of interest - why does it need to be 'random', is there some sort of security angle involved?
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
0
 

Author Comment

by:kpbarem
Comment Utility
ah well the application I am developing is using the google places API, which will return different results based on different criteria.  However, my application only displays a subset of the places generated.  That subset is supposed to change every time the application is loaded.  It may be difficult to explain without going into a lot of details.
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 84

Expert Comment

by:ozo
Comment Utility
0
 
LVL 37

Accepted Solution

by:
TommySzalapski earned 500 total points
Comment Utility
If you don't want to mess up the initial array, I would generate a list of indexes and do the first four iterations of the standard shuffle algorithm. Then the first four positions of the index array would be your items.

This is the standard shuffle algorithm that I use (where there are N items in the list)
int temp;
for (int i = 0; i < N; i++)
{
   index = RandBetween(i, N-1);
   //swap them
   temp = array[i];
   array[i] = array[index];
   array[index] = temp;
   swaper(array, i, index);
}

int RandBetween(int Min, int Max)
{
   return Min + (int)(Math.random() * ((Max - Min) + 1))
}

Open in new window


Forgive the c++ syntax. I think this is mostly the same as the way you would do it in Java.

Note that you don't need to go all the way to N. You can just go to 4.
You can see that you might swap item 0 with item 0 etc. This is fine. This algorithm gives equal chance of every possible combination.
0
 

Author Closing Comment

by:kpbarem
Comment Utility
I implemented something similar to this.  Thanks
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now