Random from set Values

Discussion in 'Plugin Development' started by ItZ_JameZ_, Apr 15, 2023.

  1. Offline

    ItZ_JameZ_

    Hello all I'm trying to make a feature where a random value gets picked from a list of predetermined values that equal 100 kinda like this

    Code:
    item1:
      chance: 10
    item2:
      chance: 10
    item3:
      chance: 15
    item4:
      chance: 35
    item5:
      chance: 30
    I'm currently using just java.util.Random to generate a random number but where I need help is doing a percent chance of getting each entry.

    EDIT: I have answered my own question I found this post on Stackoverflow and honestly its so simple. I'm embarrassed.

    https://stackoverflow.com/questions/9330394/how-to-pick-an-item-by-its-probability
    Code:
    Item A 25%
    Item B 15%
    Item C 35%
    Item D 5%
    Item E 20%
    
    Lets pretend that all the probabilities are integers, and assign each item a "range" that calculated as follows.
    
    Start - Sum of probability of all items before
    End - Start + own probability
    
    The new numbers are as follows
    
    Item A 0 to 25
    Item B 26 to 40
    Item C 41 to 75
    Item D 76 to 80
    Item E 81 to 100
    
    Now pick a random number from 0 to 100. Lets say that you pick 32. 32 falls in Item B's range.
    
    mj
     
    Last edited: Apr 17, 2023

Share This Page