Member Avatar for Member #1236512

This questions contains probability and I dont know how to work with it in Java

import numpy 
import random 

def cup_game():

    cashprice = [0.5, 1.0 , 2.0 , 5.0]

    n_turn = numpy.random.randint(5,11)
    n_mult = numpy.random.randint(3,6)

    cups = list(range(0,15))
    x = numpy.random.choice(cashprice, 15, p=[0.3, 0.4, 0.2, 0.1])
    x = list(x)

    turn = ["t" for i in range(n_turn)]
    mult = ["m" for i in range(n_mult)]

    perks = turn + mult
    perk_assignment = random.sample(cups, n_turn + n_mult)

    perk_list = [0 for i in cups]
    for i in perk_assignment:
        perk_list[i] = perks[perk_assignment.index(i)]

    picks = 5
    multiplier = 1
    winnings = 0 
    total_picks = 0

    while picks > 0 and len(x)> 0:
        choice = x.pop()
        p = perk_list.pop()
        #print("cup:", choice, "perk:", p)
        if p == "m":
            multiplier += 1
        elif p == "t":
            picks += 1
        winnings += choice 
        picks -= 1
        total_picks +=1
        #print("winnings:", winnings, "multiplier:", multiplier, "picks:", picks, "total picks:", total_picks)

    jackpot = 0 
    if total_picks == 10:
        jackpot = 20
    elif total_picks == 11:
        jackpot = 50
    elif total_picks == 12:
        jackpot = 100
    elif total_picks == 13:
        jackpot = 200
    elif total_picks == 14:
        jackpot = 500
    elif total_picks == 15:
        jackpot = 1000
    else:
        jackpot = 0

    winnings = (winnings * multiplier) + jackpot
    return winnings
n = 10000
total = 0 
for i in range(n):
    total += cup_game()

print(total/n)

Dani AI

Generated

Converting that simulation to Java is straightforward, but two things deserve attention: how the cash values are drawn with weights, and keeping cash values and perks aligned while you "draw" cups. pointed toward Java's RNG; the practical approach below pairs each cup's cash and perk in a single object, then shuffles that deck so picks remain consistent.

Make a small Cup (or record) that holds a cash value and a perk flag. Fill 15 cup objects by sampling the weighted cash distribution with a cumulative-probability helper. Decide the counts for extra-turns and multipliers, then shuffle the list and mark the first k cups as turns and the next m as multipliers. Simulate by iterating/removing from the deck, updating picks, multiplier, and winnings. Compute the jackpot after the loop with a simple map or switch.

Example Java helpers (skeletons):

static double weightedChoice(double[] values, double[] probs, Random rng) {
    double r = rng.nextDouble();
    double cum = 0.0;
    for (int i = 0; i < probs.length; i++) {
        cum += probs[i];
        if (r < cum) return values[i];
    }
    return values[values.length - 1];
}
class Cup { double cash; char perk = '-'; }

List<Cup> deck = new ArrayList<>();
for (int i = 0; i < 15; i++) {
    double c = weightedChoice(cashVals, probs, rng);
    Cup cup = new Cup(); cup.cash = c; deck.add(cup);
}
int nTurn = rng.nextInt(6) + 5; // 5..10
int nMult = rng.nextInt(3) + 3; // 3..5
Collections.shuffle(deck, rng);
for (int i = 0; i < nTurn; i++) deck.get(i).perk = 't';
for (int i = nTurn; i < nTurn + nMult; i++) deck.get(i).perk = 'm';
// simulate by popping/iterating from deck end and update picks/multiplier/winnings

Notes and troubleshooting: the original code pops cash and perk lists separately — that can misalign them unless both lists are shuffled the same way. Pairing cash+perk into one object and shuffling the deck prevents that bug. Seed the RNG for reproducible runs, and test edge cases (no perks, all perks) with small iteration counts before running large Monte Carlo trials.

You’ll find what you need in Java’s Random class (Java.util.Random)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.