Use of random

Source: Internet
Author: User

1. Random class

The default non-parametric constructor of the Random class uses the current system clock as the seed to generate pseudo-Random numbers within the required range using a series of algorithms.

12     Random rd = new Random();     int i = rd.Next();

This Random number can achieve low requirements. However, if the system clock seed obtained by the Random class is close to or even identical in high concurrency, there may be duplicates, here we use loops for example.

12345 for (int i = 0; i < 10; i++) {     Random rd = new Random();  // If no parameter is specified, the system clock is used as the seed.    Console.WriteLine(rd.Next().ToString()); }

In this example, 10 identical "random numbers" are output ".

Highlighted problem: Because Random's pseudo-Random number algorithm is fixed, the numbers calculated based on the same seed must be the same. with the speed of running modern computers, the cycle is almost completed in an instant, and the seeds are consistent, so the same random number will be output in 10 cycles.

 

Related Article

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.