Java List Create createChancePrefixList(List chances)

Here you can find the source of createChancePrefixList(List chances)

Description

Creates a list with cumulative values the specified chances creating a list of ranges.
Sample:
Item A has 10% chance
Item B has 50% chance
Item C has 40% chance
Result should be a list with values 10, 60, 100.
Generating a random number between 1 and 100 can be used to find which item has been randomly selected.
It's allowed for chances can have sum greater than 100.

License

Open Source License

Parameter

Parameter Description
chances a parameter

Declaration

public static List<Integer> createChancePrefixList(List<Integer> chances) 

Method Source Code

//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;
    }
}

Related

  1. createArrayList()
  2. createBarrier(List text)
  3. createBlankSet(String date, List titleList)
  4. createBVQueryParametersList()
  5. createCategoryName(List names)
  6. createClassList(List objects)
  7. createColumnsToken(List columnnames)
  8. createCommaList(List values, int rowCharacterCount)
  9. createCommand(LinkedList> list)

  10. HOME | Copyright © www.java2s.com 2016