Added function RandomSubset
git-svn-id: file:///humgen/gsa-scr1/gsa-engineering/svn_contents/trunk@379 348d0f76-0448-11de-a6fe-93d51630548a
This commit is contained in:
parent
b4136b6d6e
commit
f39092526d
|
|
@ -372,6 +372,24 @@ public class Utils {
|
|||
}
|
||||
|
||||
|
||||
/** Draw N random elements from list. */
|
||||
public static <T> List<T> RandomSubset(List<T> list, int N)
|
||||
{
|
||||
if (list.size() <= N) { return list; }
|
||||
|
||||
java.util.Random random = new java.util.Random();
|
||||
|
||||
int idx[] = new int[list.size()];
|
||||
for (int i = 0; i < list.size(); i++) { idx[i] = random.nextInt(); }
|
||||
|
||||
Integer[] perm = SortPermutation(idx);
|
||||
|
||||
List<T> ans = new ArrayList<T>();
|
||||
for (int i = 0; i < N; i++) { ans.add(list.get(perm[i])); }
|
||||
|
||||
return ans;
|
||||
}
|
||||
|
||||
/* TEST ME
|
||||
public static void main(String[] argv) {
|
||||
List<Integer> l1 = new LinkedList<Integer>();
|
||||
|
|
|
|||
Loading…
Reference in New Issue