范围内的随机 ThreadStatic 数字

Random ThreadStatic Number within Ranges

如何使用线程静态种子从一系列数字中生成随机数?

我需要通过多种方法在一定范围内创建随机数。问题是相同的旧副本。

我找到了一个 MSDN 博客,其中有一个很好的随机生成器,但我需要一个介于两个值之间的范围(通常为 0 - 100)。

http://blogs.msdn.com/b/pfxteam/archive/2009/02/19/9434171.aspx

public static class RandomGenNumber
    {
        private static RNGCryptoServiceProvider _global =
            new RNGCryptoServiceProvider();
        [ThreadStatic]
        private static Random _local;

        //this works
        public static int Next()
        {
            Random inst = _local;
            if (inst == null)
            {
                byte[] buffer = new byte[4];
                _global.GetBytes(buffer);
                _local = inst = new Random(
                    BitConverter.ToInt32(buffer, 0));
            }
            return inst.Next();
        }

        //this is what needs to be fixed
        public static int Next(int low, int high)
        {
            Random inst = _local;
            if (inst == null)
            {
                byte[] buffer = ????;

                _global.GetBytes(buffer);
                _local = inst = new Random(
                    BitConverter.ToInt32(buffer, 0));
            }
            return inst.Next();
        }
    } 

当我循环时,我想传递一个范围

//this is called on a loop and I want to avoid duplicates
     internal ExampleMember PopulateMember()
    {
        var member = new ExampleMember();

        member.gender = RandomGenNumber.Next(0, 100) < 42 ? "M" : "F";

        member.personalityTypeId = RandomGenNumber.Next(1, 45);

        member.age = RandomGenNumber.Next(20, 80);

        ...

我已经阅读了这里的几十篇文章,但找不到范围的示例。

生成数字的字节方式看起来很干净,但我无法让它在一个范围内工作。我应该传递整数还是加倍并乘以它?或者如何在具有开始和结束范围的 Next 的重载中使用该缓冲区值?

inst.Next() 应该是 inst.Next(low, high)。就这样。不需要触摸播种部分。将其移动到辅助方法中(例如 static Random GetThreadStaticRandom())。