1

I have 2 hashmaps.

rewards<integer,integer>
chances<integer,integer>

rewards is like : 57,500000000;9210,2500;9209,50;6660,1;6 (id-count)

chances is like : 57,95;9210,70;9209,65;6660,15; (id-chance %) id is the same both maps.

So i want to choose one id but by these chances so low chances will have lower chance to be selected. i am fully confused. thanks you! (but i need to chooce 1 id , not zero not more than 1)

guido
  • 18,864
  • 6
  • 70
  • 95
user3697574
  • 121
  • 1
  • 3

1 Answers1

0

This explains it in more detail.

int sumOfPercentages = ...; //ex) 100
Random ran = new Random();
int ranNumber = ran.nextInt(sumOfPercentages);

Integer id = -1;
int current = 0;
for (Map.Entry<Integer, Integer> entry : chances.entrySet())
{
    current += entry.getValue();
    if(ranNumber < current)
    {
      id = entry.getKey();
      break;
    }
}

System.out.println(rewards.get(id));
Community
  • 1
  • 1
Anthony Raimondo
  • 1,621
  • 2
  • 24
  • 40