解决方案 »

  1.   

    int x0 = Int32.Parse(textBox3.Text);//最小值
    int x1 = Int32.Parse(textBox1.Text);//最大值
    int x2 = Int32.Parse(textBox2.Text);//取值个数
    ListBox1.Items.AddRange(Enumerable.Range(x0, x1 - x0).OrderBy(x => Guid.NewGuid()).Take(x2).ToArray());
      

  2.   

    如果不同就替换进去,那何必判断,直接for循环把数组里所有值都替换为n不就得了
      

  3.   

       private void button1_Click(object sender, EventArgs e)
            {
                int x0 = Int32.Parse(textBox3.Text);//最小值
                int x1 = Int32.Parse(textBox1.Text);//最大值
                int x2 = Int32.Parse(textBox2.Text);//取值个数
                listBox1.Items.Clear(); //listbox刷新
                int[] num = new int[x2];
                for (int i = 0; i < num.Length; i++)
                {
                    Random rand = new Random();
                    int n = rand.Next(x0, x1);
                    if (n != num[i])
                    {
                        num[i] = n;
                        listBox1.Items.Add(num[i]);
                    }
                }
            }
      

  4.   

    顺便说下,如果要取上界
    int x1 = Int32.Parse(textBox1.Text);//最大值应该写成int x1 = Int32.Parse(textBox1.Text) + 1;//最大值
      

  5.   


    错误 2 参数 1: 无法从“int[]”转换为“object[]” D:\用户目录\Documents\Visual Studio 2010\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs 30 37 WindowsFormsApplication1
    错误 1 与“System.Windows.Forms.ListBox.ObjectCollection.AddRange(object[])”最匹配的重载方法具有一些无效参数 D:\用户目录\Documents\Visual Studio 2010\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs 30 13 WindowsFormsApplication1
      

  6.   

    ListBox1.Items.AddRange(Enumerable.Range(x0, x1 - x0).OrderBy(x => Guid.NewGuid()).Take(x2).Select(x => (object)x).ToArray());
      

  7.   

      if (n == num[i]) { }
    你这里取到一个数,而数组里已经有了,就不管了?
    你得继续取,直到取出一个数组里没有的数为止啊.
    而且每次你不能光和num[i]做比较,num[i]在数组num[ ]没有完全赋值之前,永远都是0.
    所以你需要2层循环,第一层遍历num[ ],挨个赋值
    第2层从0到i-1遍历,看是否有重复
    如果i==0,不用比较,一定没有重复(一共就一个)
      

  8.   

       private void button1_Click(object sender, EventArgs e)
            {
                int x0 = Int32.Parse(textBox3.Text);//最小值
                int x1 = Int32.Parse(textBox1.Text);//最大值
                int x2 = Int32.Parse(textBox2.Text);//取值个数
                listBox1.Items.Clear(); //listbox刷新
                int[] num = new int[x2];
                for (int i = 0; i < num.Length; i++)
                {
                    Random rand = new Random();
                    int n = rand.Next(x0, x1);
                    if (n != num[i])
                    {
                        num[i] = n;
                        listBox1.Items.Add(num[i]);
                    }
                }
            }
    我试过了还是不行哟
      

  9.   

    随机排序算法:
    先取个随机数N,作为排序的次数,循环N次
    每次取0-M(M是数组长度)之间的数,放到数组的头或尾,其他数依次移位
    这样你程序运行时间起码是可以预定的,而不是不可控的.
      

  10.   

    还有我都告诉你了,你的程序根本就运行不出来,只能死循环。因为你Next(1,3)取不到上界,只能取1或者2,你又要取3个。总之你的程序逻辑上都是错的。