Generating a txt with random numbers repeats the numbers

Asked

Viewed 112 times

1

The program has to generate 10 letters and then skip a line with 10 different letters.

I used the Random() and I was able to generate 10 random letters, only then just the lines repeat. Like, it doesn’t generate random letters, it just repeats.

I need each row a new sequence of random letters to be formed.

public void rodar()
    {
        Random dudm = new Random(DateTime.Now.Millisecond);
        StreamWriter str = new StreamWriter("arquivo.txt");
        int contador = 0;
        int num = Convert.ToInt32(textBox1.Text);
        string resultado = "";
        for (int i = 0; i < num; i++)
        {
            while (contador < 11)
            {                   
                    int rnd = dudm.Next(1, 26);
                    if (rnd == 1)
                    {
                        resultado = resultado + "a";
                        contador++;
                    }
                    else if (rnd == 2)
                    {
                        resultado = resultado + "b";
                        contador++;
                    }
                    else if (rnd == 3)
                    {
                        resultado = resultado + "c";
                        contador++;
                    }
                    else if (rnd == 4)
                    {
                        resultado = resultado + "d";
                        contador++;
                    }
                    else if (rnd == 5)
                    {
                        resultado = resultado + "e";
                        contador++;
                    }
                    else if (rnd == 6)
                    {
                        resultado = resultado + "f";
                        contador++;
                    }
                    else if (rnd == 7)
                    {
                        resultado = resultado + "g";
                        contador++;
                    }
                    else if (rnd == 8)
                    {
                        resultado = resultado + "h";
                        contador++;
                    }
                    else if (rnd == 9)
                    {
                        resultado = resultado + "i";
                        contador++;
                    }
                    else if (rnd == 10)
                    {
                        resultado = resultado + "j";
                        contador++;
                    }
                    else if (rnd == 11)
                    {
                        resultado = resultado + "k";
                        contador++;
                    }
                    else if (rnd == 12)
                    {
                        resultado = resultado + "l";
                        contador++;

                    }
                    else if (rnd == 13)
                    {
                        resultado = resultado + "m";
                        contador++;
                    }
                    else if (rnd == 14)
                    {
                        resultado = resultado + "n";
                        contador++;
                    }
                    else if (rnd == 15)
                    {
                        resultado = resultado + "o";
                        contador++;
                    }
                    else if (rnd == 16)
                    {
                        resultado = resultado + "p";
                        contador++;
                    }
                    else if (rnd == 17)
                    {
                        resultado = resultado + "q";
                        contador++;
                    }
                    else if (rnd == 18)
                    {
                        resultado = resultado + "r";
                        contador++;
                    }
                    else if (rnd == 19)
                    {
                        resultado = resultado + "s";
                        contador++;

                    }
                    else if (rnd == 20)
                    {
                        resultado = resultado + "t";
                        contador++;
                    }
                    else if (rnd == 21)
                    {
                        resultado = resultado + "u";
                        contador++;
                    }
                    else if (rnd == 22)
                    {
                        resultado = resultado + "v";
                        contador++;
                    }
                    else if (rnd == 23)
                    {
                        resultado = resultado + "w";
                        contador++;
                    }
                    else if (rnd == 24)
                    {
                        resultado = resultado + "x";
                        contador++;
                    }
                    else if (rnd == 25)
                    {
                        resultado = resultado + "y";
                        contador++;
                    }
                    else if (rnd == 26)
                    {
                        resultado = resultado + "z";
                        contador++;
                    }
            }
            str.WriteLine(resultado);
        }
        str.Close();
        if (MessageBox.Show("txt feito. Deseja vê-la?", this.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes )
        {
            Process.Start(@"arquivo.txt");
        }
        button1.Enabled = true;

    }

2 answers

4


This code has some problems. The biggest one is that it is starting the random seed every time it will use so it causes the repetition. You must trigger the seed once and then consume. You only notice it running several times.

There is a performance and consumption problem with so much concatenation, the right is to build the text, I changed it. I even cleaned the text at the end of each internal loop so as not to accumulate.

There is still a problem that if you type something other than a valid number will break your application. I made her do nothing, but you can put an error message or anything else. I created a field in the class just to test, you keep using the form object. Obviously I simplified to show the test without needing Windows Forms.

I also had it put on the screen just to test, you keep writing on the file.

And I even simplified it because you have a for inside for I don’t know why you chose a while, and has no need to do so much if, just use math.

using System;
using System.Text;
using static System.Console;

public class Program {
    public static Random Rand = new Random(DateTime.Now.Millisecond);
    public static string Text = "10";
    public static void Main() => Rodar();
    public static void Rodar() {
        if (!int.TryParse(Text, out var num)) return;
        var resultado = new StringBuilder(11);
        for (var i = 0; i < num; i++) {
            for (var j = 0; j < 11; j++) resultado.Append((char)(Rand.Next(1, 26) + 97));
            WriteLine(resultado);
            resultado.Clear();
        }
    }
}

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

1

Pay attention to your result string, it is set before entering the for loop, only that inside it is not restarted, ie:

Your loop is responsible for generating the word with 10 random letters, after you write to . txt, it starts again with the same string outworking previous, and as it already has the expected size, the program only prints again.

To fix the error just restart the result string after each write.

Obs: Your counter can be placed outside the conditionals, thus avoiding code repetition.

Browser other questions tagged

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