-
-
Save supercocoa/1c14546baf18ba1870418248c2f24dea to your computer and use it in GitHub Desktop.
Revisions
-
raws renamed this gist
Jan 24, 2012 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
raws revised this gist
Jan 24, 2012 . 1 changed file with 9 additions and 11 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,32 +1,30 @@ import java.util.NavigableMap; import java.util.Random; import java.util.TreeMap; public class WeightedCollection<E> { private NavigableMap<Integer, E> map = new TreeMap<Integer, E>(); private Random random; private int total = 0; public WeightedCollection() { this(new Random()); } public WeightedCollection(Random random) { this.random = random; } public void add(int weight, E object) { if (weight <= 0) return; total += weight; map.put(total, object); } public E next() { int value = random.nextInt(total) + 1; // Can also use floating-point weights return map.ceilingEntry(value).getValue(); } } -
raws revised this gist
Jan 24, 2012 . 1 changed file with 4 additions and 8 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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() { 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(); } -
raws revised this gist
Jan 24, 2012 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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; return blocks.ceilingEntry(value).getValue(); } -
raws created this gist
Jan 24, 2012 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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(); } }