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

random来分组之后出现大量连号
random来分组之后出现大量连号,求大神解决。一个text,两button和几个label
C# code
namespace 客栈二面摇号程序
{
    public partial class 客栈二面摇号程序 : Form
    {

        public 客栈二面摇号程序()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int number =Convert.ToInt32(textBox1.Text);
            int groups =4;
            label1.Text = "请按小组顺序入座";
            string[] strArr = Group(number, groups);
            for (int i = 0; i < strArr.Length; i++)
            {
                label2.Text += "第" + (i + 1) + "组 " + strArr[i];
            }
        }
         static string[] Group(int number, int groups)
        {
            List<int> list = new List<int>();
            int num = number / 4;
            string[] strArr = new string[groups];
            for (int i = 1; i <= number; i++)
                list.Add(i);
            for (int i = 0; i < groups; i++)
            {
                for (int j = 0; j < num; j++)
                {
                    int value = list[new Random().Next(0, list.Count)];
                    list.Remove(value);
                    strArr[i] += value.ToString("D2") + " ";
                    
                }
            }
            return strArr;
             
        }

         private void button2_Click(object sender, EventArgs e)
         {
             textBox1.Text = "";
             label2.Text = "";
         }
    }


------解决方案--------------------
把随机数据那行语句换成:
int value = list[new Random(Guid.NewGuid().GetHashCode()).Next(0, list.Count)];