'm having issues understanding what exactly this piece of code does:
1. HashMap<Integer, Integer> totals = new HashMap<>();
2.
3. for (int i = -NBR_STEPS; i <= NBR_STEPS; i++) {
4. totals.put(i, 0);
5. }
6. System.out.println("");
7. for (int i = 0; i < NBR_WALKS; i++) {
8. int total_value = 0;
9. for (int j = 0; j < NBR_STEPS; j++) {
10. int L = (int) (Math.random() * 2);
11. total_value += (L == 0) ? -1 : 1;
12. }
13. totals.put(total_value, totals.get(total_value) + 1);
14. }
15. }
What I don't understand:
- What does
totals.put(i,0)
do? - What does
total_value += (L == 0) ? -1 : 1;
do exactly? - What does
totals.put(total_value, totals.get(total_value)+1);
do?
I'm sorry that I'm asking this question, but I simply don't understand. Thank you:)