jeudi 24 octobre 2019

Inconsistent results with random rolls and some math

I'm working on making a program to roll 4 6 sided dice and do some simple math and logic with them. I was running a very rough draft of the program and started to notice that the number of rolls would be inconsistent. In particular I would sometimes wont get the smallest value or get two

If looked around for a solution online to no avail. I even copied the code from other examples on how to find the smallest value

public class test {
private static int dice(int s){
    int num = 0;
    Random random = new Random();
    num = random.nextInt(s);
    num = num + 1;
    return num;
}

public static void main(String[] args)
{
    List<Integer> rolls = new ArrayList<Integer>();
    for (int i = 0; i!=4 ; i++){
        rolls.add(dice(6));
    }
    for (Integer roll : rolls) {
        System.out.println(roll);
    }
    int min = rolls.get(0);
    int index = 0;
    for(int x = 0; x<rolls.size(); x++){
        if(rolls.get(x) < min){
           min=rolls.get(x);
           index = x;
           System.out.println("Smallest: " + min);
        }
    }

    int sum = 0;
    for (int x : rolls){
        sum += x;
    }
    System.out.println("Sum:" + sum);
}
}

This should generate 4 rolls of 6 sided dice. Then it should find the smallest value print that, then calculate the sum and print it




Aucun commentaire:

Enregistrer un commentaire