生成14个可重复一次的随机数

private int[] DealStartTiles()
    {
        //Debug.Log(System.DateTime.Now.Millisecond);
        int[] TilesPlatgame=new int[14];
        int[] NumRepeat = new int[14];
        int nraddress = 0;
        TilesPlatgame[0]=0;//init
        NumRepeat[0] = 0;
        int seedr=Mathf.RoundToInt(System.DateTime.Now.Millisecond*Random.value)/10;
        for(int i=0;i<14;i++)
        {
            while(true)
            {
                seedr=Mathf.RoundToInt(System.DateTime.Now.Millisecond*Random.value)/10;
                int Torder = 0;
                if(seedr>=0&&seedr<53)
                {
                    if(NumRepeat[0]!=0)
                    {
                        for(int k=0; k<NumRepeat.Length;k++)
                        {
                            if(seedr==NumRepeat[k])
                            {
                                Torder = 1;
                                break;
                            }else
                            {
                                Torder = 0;
                                break;
                            }
                        }
                    }
                    if (Torder == 1)
                        continue;
                    else
                        break;
                }
            }
            TilesPlatgame[i]=seedr;
            int WhoRepeat = 0;
            for(int j=0; j<i;j++)
            {
                if(TilesPlatgame[i]==TilesPlatgame[j])
                {
                    WhoRepeat = TilesPlatgame[j];
                    NumRepeat[nraddress++] = WhoRepeat;
                    break;
                }
            }
        }
        for(int l=0; l<TilesPlatgame.Length;l++)
        {
            Debug.Log(TilesPlatgame[l]);
        }
        return TilesPlatgame;
    }


总体思想就是,生成一个数放到一个数组里,然后用这个数和前面所有数进行对比,重复一次就放到另外一个数组里,下次循环其中一个分支会因为另外一个数组非空而开始进行比对,如果该随机数与重复数组重复则重新生成随机数,然后继续执行上述操作。

猜你喜欢

转载自blog.csdn.net/Caeser110/article/details/76026780