Solved

Best way to pick a random subset from a collection

Posted on 2013-12-07
6
399 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 16

Expert Comment

by:krakatoa
ID: 39703617
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
ID: 39703636
0
 

Author Comment

by:kpbarem
ID: 39703647
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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 84

Expert Comment

by:ozo
ID: 39703663
0
 
LVL 37

Accepted Solution

by:
TommySzalapski earned 500 total points
ID: 39703683
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
ID: 39703973
I implemented something similar to this.  Thanks
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A short article about a problem I had getting the GPS LocationListener working.
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

752 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