Another kind of random

This commit is contained in:
Olof Larsson 2015-04-01 12:52:13 +02:00
parent b15bf3bebb
commit 7bcc1215f1
2 changed files with 25 additions and 5 deletions

View File

@ -114,8 +114,6 @@ public class IntervalUtil
return random(min, max); return random(min, max);
} }
public static double randomDoubleFromInterval(Entry<Double, Double> interval) public static double randomDoubleFromInterval(Entry<Double, Double> interval)
{ {
double min = interval.getKey(); double min = interval.getKey();

View File

@ -1288,7 +1288,7 @@ public class MUtil
{ {
if (coll.size() == 0) return null; if (coll.size() == 0) return null;
if (coll.size() == 1) return coll.iterator().next(); if (coll.size() == 1) return coll.iterator().next();
int index = MassiveCore.random.nextInt(coll.size());
List<T> list = null; List<T> list = null;
if (coll instanceof List<?>) if (coll instanceof List<?>)
{ {
@ -1298,18 +1298,40 @@ public class MUtil
{ {
list = new ArrayList<T>(coll); list = new ArrayList<T>(coll);
} }
int index = MassiveCore.random.nextInt(list.size());
return list.get(index); return list.get(index);
} }
public static <T> List<T> random(Collection<T> coll, int count) public static <T> List<T> randomSubset(Collection<T> coll, int count)
{ {
List<T> ret = new ArrayList<T>(coll); // Clean Input
if (count < 0) count = 0; if (count < 0) count = 0;
// Create Ret
List<T> ret = new ArrayList<T>(coll);
while (ret.size() > count) while (ret.size() > count)
{ {
int index = MassiveCore.random.nextInt(ret.size()); int index = MassiveCore.random.nextInt(ret.size());
ret.remove(index); ret.remove(index);
} }
// Return Ret
return ret;
}
public static <E> List<E> random(List<E> list, int count)
{
// Create Ret
List<E> ret = new MassiveList<E>();
// Fill Ret
for (int i = 0; i < count; i++)
{
ret.add(MUtil.random(list));
}
// Return Ret
return ret; return ret;
} }