From ba07771f8333f1b098ab8a9ec9fec886b72b9cc0 Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Wed, 13 Apr 2016 16:54:12 -0400 Subject: Removed old data types --- .../main/java/bjc/utils/funcutils/ListUtils.java | 76 ++++++++++++++++++++-- 1 file changed, 72 insertions(+), 4 deletions(-) (limited to 'BJC-Utils2/src/main/java/bjc/utils/funcutils/ListUtils.java') diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcutils/ListUtils.java b/BJC-Utils2/src/main/java/bjc/utils/funcutils/ListUtils.java index 871e1cf..132e4c5 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcutils/ListUtils.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcutils/ListUtils.java @@ -1,5 +1,6 @@ package bjc.utils.funcutils; +import java.util.ArrayList; import java.util.Deque; import java.util.function.BiFunction; import java.util.function.Consumer; @@ -20,8 +21,8 @@ import bjc.utils.funcdata.IFunctionalList; public class ListUtils { private static final int MAX_NTRIESPART = 50; - private static final class TokenDeaffixer - implements BiFunction> { + private static final class TokenDeaffixer implements + BiFunction> { private String token; public TokenDeaffixer(String tok) { @@ -53,8 +54,8 @@ public class ListUtils { } } - private static final class TokenSplitter - implements BiFunction> { + private static final class TokenSplitter implements + BiFunction> { private String tokenToSplit; public TokenSplitter(String tok) { @@ -351,4 +352,71 @@ public class ListUtils { }); } } + + /** + * Select a number of random items from the list, with replacement + * + * @param + * The type of items to select + * @param list + * The list to select from + * @param numberOfItems + * The number of items to selet + * @param rng + * A function that creates a random number from 0 to the + * desired number + * @return A new list containing the desired number of items randomly + * selected from the specified list + */ + public static IFunctionalList drawWithReplacement( + IFunctionalList list, int numberOfItems, + Function rng) { + IFunctionalList selectedItems = new FunctionalList<>( + new ArrayList<>(numberOfItems)); + + for (int i = 0; i < numberOfItems; i++) { + selectedItems.add(list.randItem(rng)); + } + + return selectedItems; + } + + /** + * Select a number of random items from the list without replacement + * + * @param + * The type of items to select + * @param list + * The list to select from + * @param numberOfItems + * The number of items to selet + * @param rng + * A function that creates a random number from 0 to the + * desired number + * @return A new list containing the desired number of items randomly + * selected from the specified list without replacement + */ + + public static IFunctionalList drawWithoutReplacement( + IFunctionalList list, int numberOfItems, + Function rng) { + IFunctionalList selectedItems = new FunctionalList<>( + new ArrayList<>(numberOfItems)); + + int totalItems = list.getSize(); + + list.forEachIndexed((index, element) -> { + int winningChance = numberOfItems - selectedItems.getSize(); + // n - m + int totalChance = totalItems - (index - 1); + // N - t + + // Probability of selecting the t+1'th element + if (NumberUtils.isProbable(winningChance, totalChance, rng)) { + selectedItems.add(element); + } + }); + + return selectedItems; + } } \ No newline at end of file -- cgit v1.2.3