灰常牛逼的随机数算法

 

public List<int> CalculateRandom(int maxValue,int num)

{
            List<int> result = new List<int>();
            Random ran = new Random();
            bool isOK = false;
            while (!isOK)
            {
                int key = ran.Next(0, maxValue);// 取最大值
                if (!result.Contains(key))
                {
                    result.Add(key);
                    if (result.Count == num)// 获得了需要的数量
                    {
                        isOK = true;
                    }
                }
            }

    return result;

}

posted @ 2010-05-27 10:52  风干的记忆  阅读(523)  评论(5编辑  收藏  举报