180 if (items.isEmpty())
return null;
184 if (random !=
null) {
185 index = (int) (random.nextDouble() * items.size());
187 index = (int) (Math.random() * items.size());
189 return items.get(index);
193 if (this.random !=
null) {
194 random = this.random.nextFloat() * total;
196 random = (float) (Math.random() * total);
198 if (random > total) random = total;
201 float weightSoFar = 0f;
203 for (Float weight : weights) {
204 weightSoFar += weight;
205 if (random <= weightSoFar)
break;
208 return items.get(Math.min(index, items.size() - 1));
222 System.out.println(title);
224 Map<T, Integer> indices =
new HashMap<T, Integer>();
225 for (
int i = 0; i < items.size(); i++) {
226 T item = items.get(i);
227 indices.put(item, i);
230 List<T> sorted =
new ArrayList<T>(items);
231 Collections.sort(sorted,
new Comparator<T>() {
232 public int compare(T o1, T o2) {
233 return o1.toString().compareTo(o2.toString());
237 for (T item : sorted) {
238 int index = indices.get(item);
239 float weight = weights.get(index);
241 String percent =
"" + (int)((weight / total) * 100f) +
"%";
248 itemStr = item.toString();
250 System.out.println(String.format(
" %-30s%10s%10s", itemStr, percent,
Misc.
getRoundedValue(weight)));