• Unity中集合的随机数(指定长度—List、Dictionary)


    获取Dictionary中不重复的随机数(返回Dictionary):
            public static Dictionary RandomValues(Dictionary allDic, int randomCount)
            {
                System.Random rand = new System.Random();
                Dictionary newDic = new Dictionary();
                int size = allDic.Count;
                randomCount = randomCount > size ? size : randomCount;
                List values = Enumerable.ToList(allDic.Keys);
                while (newDic.Count < randomCount)
                {
                    TKey tk = values[rand.Next(size)];
                    if (!newDic.Keys.Contains(tk))
                    {
                        newDic[tk] = allDic[tk];
                    }
                }
                return newDic;
            }

    获取List中不重复的随机数(返回List): 

     public static List RandomValues(List allList, int randomCount)
            {
                System.Random rand = new System.Random();
                List newList = new List();
                int size = allList.Count;
                randomCount = randomCount > size ? size : randomCount;
                //List values = Enumerable.ToList(allList.Keys);
                while (newList.Count < randomCount)
                {
                    TKey tk = allList[rand.Next(size)];
                    if (!newList.Contains(tk))
                    {
                        newList.Add(tk);
                    }
                }
                return newList;
            }

  • 相关阅读:
    在Windows 10上安装单机版的Spark
    OpenGL 灰度图
    TCP的三次握手和四次挥手
    苹果手机如何设置软件提醒功能?苹果手机哪个软件有提醒功能?
    雪花是否一样问题
    快速排序算法 QuickSort algorithm
    redis详解(内部分享版)
    计算机毕业设计Java校园跑腿平台演示录像2020(源码+系统+mysql数据库+Lw文档)
    冠达管理:k线图解大全?
    深入理解地址翻译 CSAPP
  • 原文地址:https://blog.csdn.net/JLX_Sir/article/details/126126911