[Unity cainiao] generates different random numbers

Source: Internet
Author: User

1. Many Methods on the Internet use time seeds, but in a very short time, this methodIneffective

 
Random r = new random (datetime. now. millisecond); Random counter = new random (unchecked (INT) (datetime. now. ticks> CTR); Random counter = new random (system. guid. newguid (). gethashcode ());

2. Use random and hashtable to achieve the desired effect perfectly.

The following is a random generation of three codes with different positive random numbers less than 3. The result is 0 1 2, 2 0 1, and so on, there will be no repeated numbers like 0 0 1.

String teststr; void ongui () {If (guilayout. button ("generate random number") {teststr = ""; hashtable = new hashtable (); system. random Rm = new system. random (); int rmnum = 3; for (INT I = 0; hashtable. count <rmnum; I ++) {int nvalue = RM. next (3); If (! Hashtable. containsvalue (nvalue) {hashtable. add (nvalue, nvalue); // Add (Key, value) teststr + = nvalue + "" ;}} guilayout. label (teststr );}

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.