Skip to content

Instantly share code, notes, and snippets.

@supercocoa
Forked from raws/WeightedCollection.java
Created February 8, 2021 08:39
Show Gist options
  • Select an option

  • Save supercocoa/1c14546baf18ba1870418248c2f24dea to your computer and use it in GitHub Desktop.

Select an option

Save supercocoa/1c14546baf18ba1870418248c2f24dea to your computer and use it in GitHub Desktop.

Revisions

  1. @raws raws renamed this gist Jan 24, 2012. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. @raws raws revised this gist Jan 24, 2012. 1 changed file with 9 additions and 11 deletions.
    20 changes: 9 additions & 11 deletions WeightedBlockCollection.java
    Original file line number Diff line number Diff line change
    @@ -1,32 +1,30 @@
    package com.Khorn.TerrainControl.CustomObjects;

    import java.util.NavigableMap;
    import java.util.Random;
    import java.util.TreeMap;

    public class WeightedBlockCollection {
    public class WeightedCollection<E> {

    private NavigableMap<Integer, int[]> blocks = new TreeMap<Integer, int[]>();
    private NavigableMap<Integer, E> map = new TreeMap<Integer, E>();
    private Random random;
    private int total = 0;

    public WeightedBlockCollection() {
    public WeightedCollection() {
    this(new Random());
    }

    public WeightedBlockCollection(Random random) {
    public WeightedCollection(Random random) {
    this.random = random;
    }

    public void add(int weight, int[] block) {
    public void add(int weight, E object) {
    if (weight <= 0) return;
    total += weight;
    blocks.put(total, block);
    map.put(total, object);
    }

    public int[] next() {
    int value = random.nextInt(total) + 1;
    return blocks.ceilingEntry(value).getValue();
    public E next() {
    int value = random.nextInt(total) + 1; // Can also use floating-point weights
    return map.ceilingEntry(value).getValue();
    }

    }
  3. @raws raws revised this gist Jan 24, 2012. 1 changed file with 4 additions and 8 deletions.
    12 changes: 4 additions & 8 deletions WeightedBlockCollection.java
    Original file line number Diff line number Diff line change
    @@ -10,25 +10,21 @@ public class WeightedBlockCollection {
    private Random random;
    private int total = 0;

    public WeightedBlockCollection()
    {
    public WeightedBlockCollection() {
    this(new Random());
    }

    public WeightedBlockCollection(Random random)
    {
    public WeightedBlockCollection(Random random) {
    this.random = random;
    }

    public void add(int weight, int[] block)
    {
    public void add(int weight, int[] block) {
    if (weight <= 0) return;
    total += weight;
    blocks.put(total, block);
    }

    public int[] next()
    {
    public int[] next() {
    int value = random.nextInt(total) + 1;
    return blocks.ceilingEntry(value).getValue();
    }
  4. @raws raws revised this gist Jan 24, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion WeightedBlockCollection.java
    Original file line number Diff line number Diff line change
    @@ -29,7 +29,7 @@ public void add(int weight, int[] block)

    public int[] next()
    {
    int value = random.nextInt(total + 1);
    int value = random.nextInt(total) + 1;
    return blocks.ceilingEntry(value).getValue();
    }

  5. @raws raws created this gist Jan 24, 2012.
    36 changes: 36 additions & 0 deletions WeightedBlockCollection.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    package com.Khorn.TerrainControl.CustomObjects;

    import java.util.NavigableMap;
    import java.util.Random;
    import java.util.TreeMap;

    public class WeightedBlockCollection {

    private NavigableMap<Integer, int[]> blocks = new TreeMap<Integer, int[]>();
    private Random random;
    private int total = 0;

    public WeightedBlockCollection()
    {
    this(new Random());
    }

    public WeightedBlockCollection(Random random)
    {
    this.random = random;
    }

    public void add(int weight, int[] block)
    {
    if (weight <= 0) return;
    total += weight;
    blocks.put(total, block);
    }

    public int[] next()
    {
    int value = random.nextInt(total + 1);
    return blocks.ceilingEntry(value).getValue();
    }

    }