Using Random Functions in. net

Source: Internet
Author: User
Http://forum.byr.edu.cn/wForum/disparticle.php? Boardname = DOTNET & id = 980
The random number generated by the random function is always a value, and the random effect can appear in a single step tracking, which has been very strange. Code It is roughly as follows:
For (INT I = 0; I <10; I ++)
{
Random random = new random ();
Int Index = random. Next (10 );
}
The random function of. NET is related to the random seed, that is, the parameter passed to the random constructor. As shown in the code above, the default constructor is used to construct the random, and the system uses the current system time as the random number seed. However, the computer runs the above Code very quickly, the system time does not change, that is, the random number seed does not change. the random number is the same.
To solve this problem, each loop passes a different seed to the random constructor. A simple method provided on the Internet is to generate a new guid object and use its hash value (the hash value of each guid object is different) as the constructor parameter, as shown below:
For (INT I = 0; I <10; I ++)
{
Random random = new random (guid. newguid (). gethashcode ());
Int Index = random. Next (10 );
}

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.