Random of 10 Seconds

1

I have a disturbing one and that is how I can make a random event last 10 seconds, what happens is that I am developing a tombola in c # and when creating random numbers in my texboxs, only a single one generates them time. This is the Code that I use to generate the random on a button, At the end the numbers have to make a random similar to a lottery game:

            Random random1 = new Random();
            txtP1.Text = Convert.ToString(random1.Next(1, 9));
            txtP2.Text = Convert.ToString(random1.Next(0, 9));
            txtP3.Text = Convert.ToString(random1.Next(0, 9));
            txtP4.Text = Convert.ToString(random1.Next(0, 9));
            txtP5.Text = Convert.ToString(random1.Next(0, 9));
            txtP6.Text = Convert.ToString(random1.Next(0, 9));
            txtP7.Text = Convert.ToString(random1.Next(0, 9));
            txtP8.Text = Convert.ToString(random1.Next(0, 9));
            txtP9.Text = Convert.ToString(random1.Next(0, 9));
            txtP10.Text = Convert.ToString(random1.Next(0, 9));
    
asked by Julio Martinez 16.07.2017 в 22:30
source

1 answer

1

You can include your code in a while loop that counts the seconds up to ten.

System.DateTime dt = DateTime.Now.AddSeconds(10);
Random random1 = new Random();
while (dt >= DateTime.Now)
{
    txtP1.Text = Convert.ToString(random1.Next(1, 9));
    txtP2.Text = Convert.ToString(random1.Next(0, 9));
    txtP3.Text = Convert.ToString(random1.Next(0, 9));
    txtP4.Text = Convert.ToString(random1.Next(0, 9));
    txtP5.Text = Convert.ToString(random1.Next(0, 9));
    txtP6.Text = Convert.ToString(random1.Next(0, 9));
    txtP7.Text = Convert.ToString(random1.Next(0, 9));
    txtP8.Text = Convert.ToString(random1.Next(0, 9));
    txtP9.Text = Convert.ToString(random1.Next(0, 9));
    txtP10.Text = Convert.ToString(random1.Next(0, 9));
}

EDITION 1:

Since it was not exactly what you wanted, the new recommendation is that you use a timer on your form, and put it to run the tick event every 500 ms.

After that, in the tick event you should do:

txtP1.Text = Convert.ToString(random1.Next(1, 9));
txtP2.Text = Convert.ToString(random1.Next(0, 9));
txtP3.Text = Convert.ToString(random1.Next(0, 9));
txtP4.Text = Convert.ToString(random1.Next(0, 9));
txtP5.Text = Convert.ToString(random1.Next(0, 9));
txtP6.Text = Convert.ToString(random1.Next(0, 9));
txtP7.Text = Convert.ToString(random1.Next(0, 9));
txtP8.Text = Convert.ToString(random1.Next(0, 9));
txtP9.Text = Convert.ToString(random1.Next(0, 9));
txtP10.Text = Convert.ToString(random1.Next(0, 9));

You should also know when to stop ... for that you could count 20 executions of this event .. or take the time from the first event tick to the last one. o Have another separate timer that runs after 10 seconds and stop this.

    
answered by 16.07.2017 в 23:15