日期:2014-05-17  浏览次数:20987 次

最speed的方法从0~9,10个数字中随即抽取6个数字,生成一万个不重复的数
最speed的方法从0~9,10个数字中随即抽取6个数字,生成一万个不重复的数

------解决方案--------------------
C# code

            List<string> listResult = new List<string>();
            Random r = new Random();
            while (listResult.Count < 10000)
            {
                List<int> listNums = new List<int>();
                while (listNums.Count < 6)
                {
                    int i = r.Next(0, 10);
                    if (!listNums.Contains(i))
                        listNums.Add(i);
                }
                string temp = string.Join("", Array.ConvertAll<int, string>(listNums.ToArray(), Convert.ToString));
                if (!listResult.Contains(temp))
                    listResult.Add(temp);
            }
            foreach (string s in listResult)
                Console.WriteLine(s);

------解决方案--------------------
一种生成不重复数的算法 
在编程中经常遇到一些类似的问题,比如做一个双色球选号软件,其中6个双色球是从1到33之间选出6个数来,这6个数是不能重复的,这个问题就是我们今天要说的生成不重复数算法。算法描述如下:从M个数中选出N个数来(0
------解决方案--------------------
根据你提供的条件,从计算原理来分析,我认为最快的算法是生成1512000个抽取数的集合,然后随机抽取10000个数,抽取一个删除一个。
因为15:1这个比例不足够大。2^15=32768,32768<1512000,如果按照半数比较,平均也要15次以上,方能确定是否重复,如果按照黄金分割点速度快一些,但也很有限。

因此,虽然浪费了很多生成运算,但是却节省了更多的比较运算。


------解决方案--------------------
C# code
        private void button11_Click(object sender, System.EventArgs e)
        {
            //ArrayList resultList=new ArrayList();
            int number = 0;
            string ss = string.Empty;
            for(int i=0;i<10000;i++)
            {
                number = GetRandomNumber();
                while(resultList.Contains(number))
                {
                    number = GetRandomNumber();
                }
                //resultList.Add(number);
                ss += number.ToString() + "\r\n";
            }
            this.textBox2.Text = ss;
        }
        
        
        Random rnd=new Random(unchecked((int)DateTime.Now.Ticks));
        
        private int GetRandomNumber()
        {
            int number = 0,rndNumber=0;
            int[] numberList = {0, 1, 2, 3,4,5,6,7,8,9};
            ArrayList list=new ArrayList(numberList);
            string str = string.Empty;
            for(int i=0;i<6;i++)
            {
                rndNumber = rnd.Next(0, 10 - i);
                str += list[rndNumber].ToString();
                list.Remove(list[rndNumber]);
            }
            number = int.Parse(str);
            return number;
        }

------解决方案--------------------
探讨
在数组或者是各种存储里面 存储100000--999999 然后随机出一个数字 从 1000000 到 999999, 然后用这个数字 减去 100000 为数组下标 获取 存在 数组里面的值

------解决方案--------------------
用了5楼的算法
DispForm.cs
C# code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace GetRandNumber
{
    public partial class DispForm : Form
    {
        public DispForm()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            RandomNumber rn = new RandomNumber();

            rn.OnOutputMessage = DispMessage;
            rn.RandNumberCount = Convert.ToInt32(textBox1.Text);
            rn.Method1();
            rn.Method2();

        }

        private void DispMessage(string sInMessage)
        {
            StringBuilder sbMsg = new StringBuilder(textBox2.Text);
            sbMsg.AppendLine(sInMessage);
            textBox2.Text = sbMsg.ToString();
        }

    }
}