Error while using Random

Asked

Viewed 156 times

6

I set a random number to fill a fictitious table and did so:

    foreach (var usuario in LstUsuarios)
    {
        htmlUsuarios.AppendLine("<tr>");
        htmlUsuarios.AppendLine("<td>" + usuario.Nome + "</td>");
        htmlUsuarios.AppendLine("<td><strong>" + usuario.NomePerfil + "</strong></td>");
        htmlUsuarios.AppendLine("<td>" + usuario.UnidadeGerencial.Txt_Sigla_UG + "</td>");

        for (var i = 1; i <= num_periodos; i++)
        {
            rnd = new Random();
            random = rnd.Next(1, 99);
            htmlUsuarios.AppendLine("<td> " + random + "</td>");
        }
        htmlUsuarios.AppendLine("<td>100</td>");
        htmlUsuarios.AppendLine("</tr>");
    }

But the application is always showing the same number.

The curious thing is that when I debug in the visual studio it actually generates different numbers.

What is outside the red line is the part that I debugged in VS, I was checking with the breakpoints and the numbers were changing. But when I pressed keep on the VS it would generate the same number as the ones inside the red line.

I don’t understand why. Any idea?

inserir a descrição da imagem aqui

1 answer

8


Put the instância of Random() outside the for in your case:

rnd = new Random();
foreach (var usuario in LstUsuarios)
{
    htmlUsuarios.AppendLine("<tr>");
    htmlUsuarios.AppendLine("<td>" + usuario.Nome + "</td>");
    htmlUsuarios.AppendLine("<td><strong>" + usuario.NomePerfil + "</strong></td>");
    htmlUsuarios.AppendLine("<td>" + usuario.UnidadeGerencial.Txt_Sigla_UG + "</td>");

    for (var i = 1; i <= num_periodos; i++)
    {            
        random = rnd.Next(1, 99);
        htmlUsuarios.AppendLine("<td> " + random + "</td>");
    }
    htmlUsuarios.AppendLine("<td>100</td>");
    htmlUsuarios.AppendLine("</tr>");
}

Because?

How is being created the Random within the is each generated item is a new instance, and with that the initial Seed is the same, fact calculated by Enviroment.Tickcount.

About the debug:

Precisely because of the Enviroment.Tickcount will have another value in debug, and in each F11 for example, will change the millisecond calculation.

  • 3

    I would only add one detail to say that this happens because as an instance of Random is created at each iteration, the seed will be the same (without passing a seed the Random uses the Enviroment.TickCount) and hence have repeated numbers. Behavior was not observed during Debug because the code will run more slowly which allows to have seeds different.

  • @Omni that very thing !!!

  • And you have an idea why when I’m debugging it generates different numbers?

  • 1

    Juxtaposition because of Enviroment.Tickcount will have another value in the debug, and in each F11 for, for example, it will change the millisecond calculation

  • 1

    @Joaopaulo "The behavior was not observed during Debug because the code will be executed more slowly which allows to have different Eds." This slowness may come from the user debugging step by step or from the simple overhead caused by Debugger.

Browser other questions tagged

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