0 Replies - 840 Views - Last Post: 08 May 2012 - 07:43 PM Rate Topic: -----

#1 jjkoletar  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 03-May 12

Weighted randomness gone wrong

Posted 08 May 2012 - 07:43 PM

I frequent the Bukkit plugin community (Bukkit is a Minecraft server API), as I develop a few Bukkit plugins (in java) myself. I recently came across a new plugin that performs the task of setting an area of blocks to weighted values. Aka 50% Stone, 50% sand, etc. The implementation... burns:
public class RandomBlockGenerator {
 
    List<String> bin;
    
    public RandomBlockGenerator (List<String> blockList, List<String> weightList)
    {
    	bin = new ArrayList<String>(100000);
    	
    	int counter = 0;
    	for(int i = 0; i < blockList.size(); i++)
    	{
    		for(int j = 0; j < (Double.parseDouble(weightList.get(i)) * 1000); j++)
    		{
    			bin.add(blockList.get(i));
    			counter++;
    		}
			counter = 0;
    	}
    }
    
    public int next()
    {
    	Random seed = new Random();
    	
    	int rand = Integer.parseInt(bin.get(seed.nextInt(bin.size())));
    	return rand;
    }
}



Numbers stored as strings, repeatedly parsed to their numeric types, a 100,000 element string array... It's not good.

Is This A Good Question/Topic? 0
  • +

Page 1 of 1