为了账号安全,请及时绑定邮箱和手机立即绑定

C#随机取数怎样消除重复数字(求代码)?

C#随机取数怎样消除重复数字(求代码)?

月关宝盒 2018-11-06 06:07:00
C#随机取数消除重复数字的方法(求代码)。
查看完整描述

2 回答

?
繁花不似锦

TA贡献1851条经验 获得超4个赞

 class UniqueRandom : Random
    {
        Dictionary<int, int> _tbl = new Dictionary<int, int>();

        new public int Next()
        {
            int ret = base.Next();

            while (_tbl.ContainsKey(ret))
            {
                ret = base.Next();
            }

            _tbl.Add(ret, 0);

            return ret;
        }

        new public int Next(int maxValue)
        {
            if (_tbl.Count >= maxValue)
            {
                throw new Exception("Overflow!");
            }

            int ret = base.Next(maxValue);

            while (_tbl.ContainsKey(ret))
            {
                ret = base.Next(maxValue);
            }

            _tbl.Add(ret, 0);

            return ret;
        }

        new public int Next(int minValue, int maxValue)
        {
            if (_tbl.Count >= maxValue - minValue)
            {
                throw new Exception("Overflow!");
            }

            int ret = base.Next(minValue, maxValue);

            while (_tbl.ContainsKey(ret))
            {
                ret = base.Next(minValue, maxValue);
            }

            _tbl.Add(ret, 0);

            return ret;
        }
    }

 

        static void Main(string[] args)
        {
            UniqueRandom rand = new UniqueRandom();

            for (int i = 0; i < 100; i++)
            {
                Console.WriteLine(rand.Next(100));
            }

        }

 

下面是改用 HashSet的,比Dictionaly 要好,但必须是 .Net3.5才能支持

using System;
using System.Collections.Generic;
using System.Text;

namespace TestConsole
{
    class UniqueRandom : Random
    {
        System.Collections.Generic.HashSet<int> _tbl = new HashSet<int>();

        new public int Next()
        {
            int ret = base.Next();

            while (_tbl.Contains(ret))
            {
                ret = base.Next();
            }

            _tbl.Add(ret);

            return ret;
        }

        new public int Next(int maxValue)
        {
            if (_tbl.Count >= maxValue)
            {
                throw new Exception("Overflow!");
            }

            int ret = base.Next(maxValue);

            while (_tbl.Contains(ret))
            {
                ret = base.Next(maxValue);
            }

            _tbl.Add(ret);

            return ret;
        }

        new public int Next(int minValue, int maxValue)
        {
            if (_tbl.Count >= maxValue - minValue)
            {
                throw new Exception("Overflow!");
            }

            int ret = base.Next(minValue, maxValue);

            while (_tbl.Contains(ret))
            {
                ret = base.Next(minValue, maxValue);
            }

            _tbl.Add(ret);

            return ret;
        }
    }
}


查看完整回答
反对 回复 2018-11-07
?
饮歌长啸

TA贡献1951条经验 获得超3个赞

IList<int> list = new List<int>();
            Random rd = new Random();
            for (int i = 0; i < 100; i++)
            {
                int num = rd.Next(0, 100);
                if (!list.Contains(num))
                {
                    list.Add(num);
                }
            }
            foreach (int i in list)
            {
                Console.WriteLine(i);
            }


查看完整回答
反对 回复 2018-11-07
  • 2 回答
  • 0 关注
  • 338 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信