Create a program that draws 6 numbers and shows how many times it was drawn

Asked

Viewed 177 times

7

I’m making a program that draws 6 numbers (1 to 6) 1 million times and in the end shows how many times was drawn, but I have a doubt in the code, I’m optimizing in vectors not to use if, but in the end it is considering the position 0 of the vector and the number 0 and ends up not drawing the number 6, only from 0 to 5, can help me see what is wrong?

Follow the code in C#:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)

        {

            int[] numeros;
            numeros = new int[6];
            int randnum;
            Random rdm = new Random();

            for (int i = 0; i < 1000000; i++)

            {

                randnum = rdm.Next(6);
                Console.WriteLine("girei pela: " + i + " vez");

                numeros[randnum] = numeros[randnum] + 1;

            }


            for (int i = 0; i < numeros.Length; i++)
            {

                Console.WriteLine("Quantidade de vezes que o N° "+i+ " foi sorteado: " + numeros[i]);

            }

            Console.ReadKey();

        }
    }
}

1 answer

7


Just add one up:

randnum = rdm.Next(6) + 1;

The code generator generates a number of 0 until just before the number you placed in the method Next(). It’s like a array, the number you put there doesn’t count. Since you also don’t want 0 just add 1, then what you were giving from 0 to 5 starts generating from 1 to 6.

But if you analyze the code better, it’s not even a case of doing it. You should only add one in the presentation. I explain: array It goes from 0 to 5, and you’re drawing these numbers, so that’s correct. When presenting what is in the index 0 you want to consider that is the number 1, and what is in 1 you want to show as 2, so on until the last element of the array which is 5 and you want to present as 6 drawn.

And you can optimize some things in the code.

using System;
using static System.Console;

namespace ConsoleApplication1 {
    public class Program {
        public static void Main() {
            var numeros = new int[6];
            var rdm = new Random();
            for (int i = 1; i <= 1_000_000; i++) {
                WriteLine($"girei pela: {i}a. vez");
                numeros[rdm.Next(6)]++;
            }
            for (var i = 0; i < numeros.Length; i++) WriteLine($"Quantidade de vezes que o N° {i + 1} foi sorteado: {numeros[i]}");
        }
    }
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

  • 1

    Gee, I can understand! Thanks a lot for the help... I’m getting into this language now and I’m getting deeper. This part you put "1_000_000" separately, is for better reading of the number only? What is the function of "$" before the text?

  • Yes, I used separator to give more readability. See https://answall.com/q/91117/101

Browser other questions tagged

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