Solved Selecting random element from hashmap with probabilities

Discussion in 'Plugin Development' started by 1Camer0471, Sep 7, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    1Camer0471

    Title says it all, how can this be done?
    The hashmap looks like HashMap<ItemStack, Integer>, Integer being the probability of that itemstack being selected.
     
  2. Offline

    Zombie_Striker

    @1Camer0471
    1. Get the itemstack,
    2. get that integer
    3. Do a random with the max value being (100)
    4. If the random number it returns is less than the first integer, return true
    5. else, return false.
     
    1Camer0471 likes this.
  3. Offline

    1Camer0471

    Yes but there are more then one item with say 10 and its probability, so how would I handle that?
     
  4. Offline

    Zombie_Striker

    @1Camer0471
    In that case, hashmaps wont work (way I suggested was a terrible way, but it was the only one that would work with what you have)
    1. Create an Hashmap that hold Integers and itemstacks (instead of the other way around)
    2. Add an itemstack to the hashmap over and over again until you get the chances right (100 objects in hashmap, you want Snowballs to be 10% chance. Add snowballs 10 times)
    3. Use a random number to get an itemstack (from 0 to 100) Hashgmap.get(randomnumber)//returns itemstack
     
    1Camer0471 likes this.
  5. Offline

    teej107

    @1Camer0471 It's a Map. As long as you have the ItemStack, you can get the probability (unless the ItemStack isn't added as a key).
     
    1Camer0471 likes this.
Thread Status:
Not open for further replies.

Share This Page