A
A
Anton2018-01-15 09:47:16
Java
Anton, 2018-01-15 09:47:16

How to randomly generate a certain number of numbers?

I have the number of people (133) and the number of floors (12). And I need to randomly but evenly "place" these people on the floors, i.e. 12(1st floor), 8(2nd floor), 9, 11, 14...

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Oparin, 2018-01-15
@losse_narmo

random but uniform

So randomly or uniformly?
I assume that you will be satisfied with the following algorithm:
For each person (out of 133) in the cycle, randomly select a floor (from 1 to 12).
For each floor, we summarize how many people got there.

S
Sanan Yuzb, 2018-01-15
@Sanan07

HashMap<Integer, Integer> map = new HashMap<>();
    
    for (int i = 1; i <=12; i++) {
      map.put(i, 0);
    }
    
    for (int i = 1; i <= 133; i++) {
      int floor = (int) (1 + Math.random()*12);
      map.put(floor, map.get(floor).intValue()+1);
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question