C#随机双色球

using System;
using System.Collections.Generic;

namespace ConsoleApplicationRandnumber
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> arr = new List<int>(6);
            arr = redNumber();
            Console.Write("红色球随机:
");
            for (int i = 0; i < 6; i++)
            {
                Console.Write("{0}"+"  ",arr[i]);
            }
            int bNumber = blueNumber();
            Console.WriteLine("
蓝色球随机:
{0}",bNumber);
            Console.ReadLine();
        }
        
        /// <summary>
        /// 从1到33中选取不重复的6个随机数
        /// </summary>
        /// <returns></returns>
        public static List<int> redNumber()
        {
            //用于存放1-33这33个数
            List<int> container = new List<int>(33);
            //用于保存返回结果
            List<int> result = new List<int>(6);
            Random random = new Random();
            for (int i = 1; i <= 33; i++)
            {
                container.Add(i);
            }
            int index = 0;
            int value = 0;
            for (int i = 1; i <= 6; i++)
            {
                //从[0,container.Count]中取出一个值,爆炸这个值不会超过container的元素个数
                index = random.Next(0, container.Count);
                //以随机生成的值作为索引取container中的值
                value = container[index];
                //将取出的随机值放到结果集中
                result.Add(value);
                //从容器中删除这个值,这样会导致container.count发生变化
                container.Remove(index);
            }
            result.Sort();
            return result;
        }
        public static int blueNumber()
        {
            Random random = new Random();
            int bNumber = random.Next(0, 16);
            return bNumber;
        }
    }
}

  

原文地址:https://www.cnblogs.com/tangt/p/3922438.html