文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>生成100个不重复的随机数算法

生成100个不重复的随机数算法

时间:2010-11-18  来源:chenping2008

代码如下:

 Int32 count = 0;
            Dictionary<Int32,IList<Int32>> results=new Dictionary<int,IList<int>>();
            while (count < 100)
            {
                while (true)
                {
                    Int32 rand = new Random().Next(1, 1000);
                    Int32 mod = rand % 10;
                    if (results.Keys.Contains(mod))
                    {
                        if (!results[mod].Contains(rand))
                        {
                            results[mod].Add(rand);
                            count++;
                            break;
                        }
                    }
                    else
                    {
                        IList<Int32> lists = new List<Int32>();
                        lists.Add(rand);
                        results.Add(mod, lists);
                        count++;
                        break;
                    }
                }
            }
            //output
            foreach (Int32 k in results.Keys)
            {
                foreach (Int32 v in results[k])
                {
                    Response.Write(v.ToString()+"  ");
                }
            }
            Response.Write("<br/>"+count.ToString());
            Response.Flush();
            Response.End(); 

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载