Probability in data postings

3

Hello, guys! I'm learning to program java and I came across two exercises: in the first one, I should create a script that mimics a dice roll; in the second, a script that would cast the dice 1 million times and show how many times each number was drawn. Here is 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 overwhelmed with the result of successive launches: the number 6 is always drawn approximately half the time 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? Thank you in advance! Hugs!

    
asked by anonymous 27.08.2016 / 00:53

1 answer

1

In fact, number 6 has not been drawn%% of% times, it's just an error in counting information: note that 833116 refers only to conditional else , ie, if (num == 5) is < 2 , 3 , 4 or 6 , the program will add 1 to num , resulting in a much larger number.

By the way, in your example, the correct value of count6 would be count6 , a normal value.

To solve this, you can simply replace 1000000 - 166914 - 166765 - 167124 - 165377 - 166884 = 166936 with else or you can change each of if (num == 6) and if after else to if (num == 1) , where else if (num == x) goes from 2 to 6 .

    
27.08.2016 / 02:46