diff options
| author | Benjamin J. Culkin <bjculkin@mix.wvu.edu> | 2018-06-04 17:00:17 -0300 |
|---|---|---|
| committer | Benjamin J. Culkin <bjculkin@mix.wvu.edu> | 2018-06-04 17:00:17 -0300 |
| commit | 6dc989c9768979d7bc1745c641f0354eea9c3558 (patch) | |
| tree | f2efd42588a4e81573975084ec0a6c623d968927 /base/src/main/java/bjc/utils | |
| parent | 8dfddc586f49b2d7ce4a4d712334a28584c0b315 (diff) | |
Update
Diffstat (limited to 'base/src/main/java/bjc/utils')
| -rw-r--r-- | base/src/main/java/bjc/utils/gen/WeightedRandom.java | 50 | ||||
| -rw-r--r-- | base/src/main/java/bjc/utils/ioutils/blocks/Block.java | 14 |
2 files changed, 60 insertions, 4 deletions
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<E> { /* 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<E> { * @param src * The source of randomness to use. */ - public WeightedRandom(final Random src) { + public WeightedRandom(Random src) { probabilities = new FunctionalList<>(); results = new FunctionalList<>(); @@ -45,6 +47,12 @@ public class WeightedRandom<E> { } /** + * Create a new weighted random generator. + */ + public WeightedRandom() { + this(BASE); + } + /** * Add a probability for a specific result to be given. * * @param chance @@ -71,7 +79,6 @@ public class WeightedRandom<E> { public E generateValue(Random rn) { int target = rn.nextInt(totalChance); - int i = 0; for(int prob : probabilities) { @@ -101,4 +108,43 @@ public class WeightedRandom<E> { public IList<IPair<Integer, E>> 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()); + } } } |
