比较我要生成(1,7)之间所有的随机数,然后存储到一个数组中,而且要求不能相同,请问该如何写啊?找了以前别人的帖子,找到的方法不可用,调试之后发现还是有一样的。在线等。写论文用。

解决方案 »

  1.   

    Random.Sample 方法  [C#]示例
    本例使用 Rnd 函数生成一个 1 到 6 之间的随机整数值。Dim MyValue As Integer
    MyValue = CInt(Int((6 * Rnd()) + 1)) ' Generate random value between 1 and 6.
      

  2.   

    private void button1_Click(object sender, EventArgs e)
    {
        Random x = new Random();    for(int i=0;i<7;i++)
            listBox1.Items.Add(x.Next(1,8));
    }
    我刚写的一小段代码,看你能不能用.
    private void button1_Click(object sender, EventArgs e)
    {
        Random x = new Random();
        int[] arr = new int[7];    for (int i = 0; i < 7; i++)
            arr[i] = x.Next(1, 8);    for (int i = 0; i < 7; i++)
            listBox1.Items.Add(arr[i]);
    }
      

  3.   

    洗牌算法:
    int i;
    for(int i=0;i<7;i++)
    {
       a[i]=i;
    }
    Random r = new Random();
    for(i=0;i<7;i++)
    {
        int j=r.Next(i,7);
        temp = a[i];
        a[i] = a[j];
        a[j] = temp;
    }
      

  4.   

    我觉得可以先用一个容器存储1-7这七个数,比如数组,然后不断随机生成一个数,与该容器内的数比较,相等就剔除该数,同时把该数存入另一个数组中int[] becmp=new int[7]{0,0,0,0,0,0,0}
    int[] save=new int[7]{0,0,0,0,0,0,0}
    int i=0;
    do
    {
    Random r = new Random();
    int j=r.Next(1,8);if(becmp[j-1]==0)
    {
       becmp[j-1]=1;
       save[i]=j;
       i++;
    }
    }
    while(i<7)