. Net to generate random numbers that are not repeated

Source: Internet
Author: User
Tags random seed
Int Myran =   10 ; // Initialize myran
Public   Static   Int Genforint ( Int Minvalue, Int Maxvalue, Ref   Int Myran) // Auto generate random int data
{
Random ran =   New Random ();
Int Randkey;
Do  
{
Randkey = Ran. Next (minvalue, maxvalue );
} While (Randkey = Myran );
Console. writeline ( " Randkey: "   + Randkey );
Myran = Randkey; // Assign this random value to myran
Return Randkey;
}

Let's talk about random numbers first.AlgorithmThe implementation of the C-numeric algorithm: the use of computer, this kind of human design
The machines with the most accurate and accurate judgment can generate random numbers.
It cannot be understood in terms of concept.ProgramIt will certainly produce completely predictable results, so it is not a real "random number ".

The "random number" produced by the random number generation function in various languages is actually called "pseudo-random number ".
The random number function is considered as an expression:

A = R (s)

Here, R is a random function, and S is a seed. A is a series. That is, for any seed s, after r calculation, there is always a definite
And when var RND = new random (s) is called in C # Or srand (s) is called in C
One of the tasks is to set this seed, and RND. Next (); or Rand () is just to get the next element on A. Of course, the actual
It is impossible to calculate a series A in advance, so rand () is equivalent to calculating the next number S by S, and then using s as the new
The seed of is assigned to S, and then returns the result.

**-------------------------
The following are two common errors:

For ( Int I = 0 ; I < N; ++ I)
{
VaR RND =   New Random (s ); // S is a number first determined.
Console. Write ( " {0 }, " , RND. Next ());
}

In this way, the random number generator will only generate a fixed constant n.
Because each time a random number generator is initialized with the same seed, next () is called ().
All obtained are the first element in Series A, and the value of this element must be fixed.
(Of course, N depends on the implementation of the random function ).

The second case is more common:

For ( Int I = 0 ; I < N; ++ I)
{
VaR RND =   New Random (); // Use System time as the seed
Console. Write ( " {0 }, " , RND. Next ());
}

In this way, we hope to achieve random effects through different times, but the result is the same as that of my friend.
97,97, 97,97,... 97,30, 30,30, 30,30, 30,30, 30,30, 30,30, 30,30,..., 27,27, 27,27, 27,27 ,....
This is because the update frequency of the Windows system clock is about 10 ms, and the execution of this for loop is obviously faster.
So in a period of execution time, environment. tickcount (default Random Seed) or the time function of C returns
All are the same value. As a result, RND. Next returns a constant within a period of time.

PS: in a program that frequently uses random numbers, you can initialize a global random number generator.
Next is called directly when a random number is used, instead of constructing a random every time.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.