Here you can find the source of createChancePrefixList(List
Parameter | Description |
---|---|
chances | a parameter |
public static List<Integer> createChancePrefixList(List<Integer> chances)
//package com.java2s; /**/*from w w w .j a va 2 s . c om*/ * Random Pipes: Java application based on the game Pipe Dream * * Copyright (C) 2015 Zdravko Petkov * * This program is free software: you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software * Foundation, either version 3 of the License, or any later version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * this program. If not, see <http://www.gnu.org/licenses/>. */ import java.util.ArrayList; import java.util.List; public class Main { /** * Creates a list with cumulative values the specified chances creating a * list of ranges.<br> * Sample:<br> * Item A has 10% chance<br> * Item B has 50% chance<br> * Item C has 40% chance<br> * Result should be a list with values 10, 60, 100.<br> * Generating a random number between 1 and 100 can be used to find which * item has been randomly selected.<br> * It's allowed for chances can have sum greater than 100. * * @param chances * @return */ public static List<Integer> createChancePrefixList(List<Integer> chances) { List<Integer> result = new ArrayList<Integer>(); Integer range = 0; for (Integer chance : chances) { range = range + chance; result.add(range); } return result; } }