Detailed code example of C # generating random number function

Source: Internet
Author: User
This article mainly introduces the C # generation of random number function, involving C # mathematical operations and string manipulation related skills, with a certain reference value, the need for friends can refer to the following

The example in this article describes the C # generation of random numbers functionality. Share to everyone for your reference, as follows:

Using system;using system.collections.generic;using system.linq;using system.text;namespace csharp{class Program {s      tatic void Main (string[] args) {Console.WriteLine ("Generate random number \ n");      int randcount = 9;//number of random digits int randmin = 1;//random number minimum int randmax = 21;//random number max int randindex, flag, temp;      Randindex = TEMP = flag = 0;      Random rand = new Random ();      int[] Randarr = new Int[randcount]; RANDARR[0] = rand.      Next (Randmin, Randmax);        while (true) {flag = 0; temp = rand.        Next (Randmin, Randmax);            for (int i = 0; I <= randindex; i++) {if (temp = = Randarr[i]) {flag = 1;          Break        }} if (flag = = 1)//IF flag = = 1 has duplicate number generation.        {continue;          } else if (flag = = 0) {randindex++;        Randarr[randindex] = temp;        } if (Randindex >= randCount-1)//If the Randcount exit loop is reached {break;}} for (int i = 0; i < Randcount; i++) {Console.WriteLine ("arr[" + i + "]=" + randarr[i]); } Console.WriteLine ("\ n any key exits.      ");    Console.ReadLine (); }  }}

Generate random numbers with no duplicates

The results of the operation are as follows:

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.