From 6dc989c9768979d7bc1745c641f0354eea9c3558 Mon Sep 17 00:00:00 2001 From: "Benjamin J. Culkin" Date: Mon, 4 Jun 2018 17:00:17 -0300 Subject: Update --- .../main/java/bjc/utils/gen/WeightedRandom.java | 50 +++++++++++++++++++++- .../main/java/bjc/utils/ioutils/blocks/Block.java | 14 +++++- 2 files changed, 60 insertions(+), 4 deletions(-) (limited to 'base/src/main/java') diff --git a/base/src/main/java/bjc/utils/gen/WeightedRandom.java b/base/src/main/java/bjc/utils/gen/WeightedRandom.java index c7cee19..2357c96 100644 --- a/base/src/main/java/bjc/utils/gen/WeightedRandom.java +++ b/base/src/main/java/bjc/utils/gen/WeightedRandom.java @@ -28,6 +28,8 @@ public class WeightedRandom { /* The total chance for all values. */ private int totalChance; + private final static Random BASE = new Random(); + /** * Create a new weighted random generator with the specified source of * randomness. @@ -35,7 +37,7 @@ public class WeightedRandom { * @param src * The source of randomness to use. */ - public WeightedRandom(final Random src) { + public WeightedRandom(Random src) { probabilities = new FunctionalList<>(); results = new FunctionalList<>(); @@ -44,6 +46,12 @@ public class WeightedRandom { source = src; } + /** + * Create a new weighted random generator. + */ + public WeightedRandom() { + this(BASE); + } /** * Add a probability for a specific result to be given. * @@ -71,7 +79,6 @@ public class WeightedRandom { public E generateValue(Random rn) { int target = rn.nextInt(totalChance); - int i = 0; for(int prob : probabilities) { @@ -101,4 +108,43 @@ public class WeightedRandom { public IList> getValues() { return probabilities.pairWith(results); } + + public E getDescent(int factor) { + return getDescent(factor, source); + } + + public E getDescent(int factor, Random rn) { + for(E res : results) { + if(rn.nextInt(factor) == 0) continue; + + return res; + } + + return results.getByIndex(results.getSize() - 1); + } + + public E getBinomial(int target, int bound, int trials) { + return getBinomial(target, bound, trials, source); + } + + public E getBinomial(int target, int bound, int trials, Random rn) { + int numSuc = 0; + + for(int i = 0; i < trials; i++) { + /* + * Adjust for zero, because it's easy to think of this + * as rolling a bound-sided dice and marking a success + * for every roll less than or equal to target. + */ + int num = rn.nextInt(bound) + 1; + + if(num <= target) { + //System.err.printf("\t\tTRACE: mark binomial success (%d <= 1d%d, %d)\n", target, bound, num); + numSuc += 1; + } + } + + //System.err.printf("\tTRACE: got %d success for binomial trials (%d <= 1d%d, %d times)\n", numSuc, target, bound, trials); + return results.getByIndex(Math.min(numSuc, results.getSize() - 1)); + } } diff --git a/base/src/main/java/bjc/utils/ioutils/blocks/Block.java b/base/src/main/java/bjc/utils/ioutils/blocks/Block.java index 1bf6b46..61d473c 100644 --- a/base/src/main/java/bjc/utils/ioutils/blocks/Block.java +++ b/base/src/main/java/bjc/utils/ioutils/blocks/Block.java @@ -27,6 +27,8 @@ public class Block { */ public final int blockNo; + public int lineOffset; + /** * Create a new block. * @@ -81,8 +83,16 @@ public class Block { @Override public String toString() { - String fmt = "Block #%d (from lines %d to %d), length: %d characters"; + if(lineOffset != -1) { + String fmt = "Block #%d (from lines %d (%d) to %d (%d)), length: %d characters"; + + return String.format(fmt, blockNo, startLine + lineOffset, + startLine, endLine + lineOffset, + endLine + lineOffset, contents.length()); + } else { + String fmt = "Block #%d (from lines %d to %d), length: %d characters"; - return String.format(fmt, blockNo, startLine, endLine, contents.length()); + return String.format(fmt, blockNo, startLine, endLine, contents.length()); + } } } -- cgit v1.2.3