Probability in data launches

Asked

Viewed 295 times

3

Hey there, you guys! I’m learning to program java and came across two exercises: in the first, I should create a script that simulates a roll of a dice ; in the second, a script that would roll the die 1 million times and show how many times each number was drawn. Follow the script below:

package dado;
import java.util.Scanner;
import java.util.Random;

public class Dado
{
    public static int dado()
    {
        Random rand = new Random();
        return rand.nextInt(6)+1;
    }
    public static void main(String[] args)
    {
        Scanner entrada = new Scanner(System.in);

        System.out.println("Bem vindo ao cassino! Deseja rolar o dado? [y/n]");
        char resp = entrada.nextLine().charAt(0);
        if (resp == 'y')
        {
            System.out.println("Resultado: " + dado());
            do
            {
                System.out.println("Deseja rolar o dado novamente? [y/n]");
                resp = entrada.nextLine().charAt(0);
                switch (resp)
                {
                    case 'y':
                        System.out.println("Resultado: " + dado());
                        break;
                    case 'n':
                        System.out.println("Finalizando...");
                        break;
                    default:
                        System.out.println("Resposta inválida. Responda apenas sim (y) ou não (n)");
                }
            }while(resp == 'y');
        }
        else if(resp == 'r')
        {
            int count1 = 0,
                count2 = 0,
                count3 = 0,
                count4 = 0,
                count5 = 0,
                count6 = 0;
            for(int i = 1; i <= 1000000; i++)
            {
                int num = dado();
                if (num == 1)
                {
                    count1++;
                }
                if (num == 2)
                {
                    count2++;
                }
                if (num == 3)
                {
                    count3++;
                }
                if (num == 4)
                {
                    count4++;
                }
                if (num == 5)
                {
                    count5++;
                }
                else
                {
                    count6++;
                }
            }
            System.out.printf("O dado rolou o número 1 %d vezes.\n", count1);
            System.out.printf("O dado rolou o número 2 %d vezes.\n", count2);
            System.out.printf("O dado rolou o número 3 %d vezes.\n", count3);
            System.out.printf("O dado rolou o número 4 %d vezes.\n", count4);
            System.out.printf("O dado rolou o número 5 %d vezes.\n", count5);
            System.out.printf("O dado rolou o número 6 %d vezes.\n", count6);
        }
        else
        {
            System.out.println("Finalizando...");
        }
    }
}

The code worked correctly. However, I was surprised by the result of the successive releases: the number 6 is always drawn approximately half the time as the others. EX:

O dado rolou o número 1 166914 vezes.
O dado rolou o número 2 166765 vezes.
O dado rolou o número 3 167124 vezes.
O dado rolou o número 4 165377 vezes.
O dado rolou o número 5 166884 vezes.
O dado rolou o número 6 833116 vezes.

Why does this happen? Thanks in advance! Hug!

1 answer

1


Actually, number 6 was not drawn 833116 sometimes, it’s just an error in accounting information: note that the elserefers only to the conditional if (num == 5), that is, if num be it 1, 2, 3, 4 or 6, the program will add 1 to the count6, resulting in a much larger number.

By the way, in your example, the correct value of count6 would be 1000000 - 166914 - 166765 - 167124 - 165377 - 166884 = 166936, a normal value.

To solve this, you can simply replace the else for if (num == 6) or you can trade each of the if and else after the if (num == 1) for else if (num == x), in which x goes from 2 to 6.

  • Wow, man, I didn’t touch that! Thanks so much for the help, I’m gonna fix the code. Hug!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.