Get the App
SLTechnology News&Howtos  ›  Development  › 

How to write a Class Roller with Random numbers in C #

Shulou Source: shulou.com Published: 2022-05-31 18:15:25 10月01日 Update

Most people do not understand the knowledge points of this article "how to use random numbers to write a class roll call machine", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article, let's take a look at this "C# how to use random numbers to write class roll call" article.

English meaning

What can random numbers do?

Generating random numbers has a wide range of uses, such as randomly selecting a record from a database, giving the generated numbers to variables, executing some code at a certain point in time, assigning random numbers to arrays, and so on.

Class roll call

For example, there is a form that requires roll call according to the seat number.

1. Create a Windows project

And then the next step is done.

two。 Drag control

If there is no toolbox in the view, it can be called up.

3. Write code

The code in the button

Random a = new Random (); / / create a random number string [] name = new string [6]; / / create a string numeric storage name name [1] = "Cai Xule"; name [2] = "Lubunda"; name [3] = "Li Yunyu"; name [4] = "Ma Dong Tu"; name [5] = "Bai Jukao" / / assign values to each element, / / the number in / / [] is their seat number / / that is, the number of elements in the array int b = a.Next (1Lame.Length); / / A random number is assigned to the b variable, returning a random number from 1 to 6 / / name.Length: the length of the array, which is as long as there are several elements

The array starts at 0 because you need to call the roll by seat number.

So the array needs more than 1 number, such as 60 people, then your array has to write 61.

Int b = a.Next (1dyname.length); / / Random number is assigned to variable b, returning a random number from 1 to 6 / / name.Length: the length of the array, as long as there are several elements.

Ok, run

Effect picture:

Each click is a different person.

Other uses

Random interval time

Random a = new Random (); int b = a.Next (1000, 5000); Thread.Sleep (b); MessageBox.Show ("Little Horse's coffin plate"); / / execute the window sentence at random intervals of 1-5 seconds

Random data extraction from database

Random a = new Random (); int b = a.Next (1, 5000); string str = "SELECT * FROM Table_2 WHERE id ='" + b + ""; above is the content of this article on "how to write a class roll call machine with random numbers". I believe you all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about it, please pay attention to the industry information channel.

Tags: Random numbers arrays content classes elements numbers lengths codes variables data that is seats seat numbers databases articles time uses knowledge articles Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info macOS Shulou Information OPPO Reno Redmi