C# 在.NET中生成随机数而不使用任何内置函数

C# 在.NET中生成随机数而不使用任何内置函数,c#,.net,vb.net,random,C#,.net,Vb.net,Random,我想知道每种编程语言中的随机函数是如何工作的,所以我想自己生成一个数字,也就是说,我不想使用任何内置的类或函数。如果你想知道它是如何工作的,你可以从Wikipedia开始:和。第二个链接将为您提供一些可以自己实现的流行算法(如Mersenne Twister) 您还可以使用.Net Reflector反编译System.Random,并将给定的算法与.Net中本机实现的算法进行比较 另外,D.Knuth有一章是关于随机数及其生成的。正如其他人所评论的,你真的想依靠框架功能来实现这一点。如果这是出

我想知道每种编程语言中的随机函数是如何工作的,所以我想自己生成一个数字,也就是说,我不想使用任何内置的类或函数。

如果你想知道它是如何工作的,你可以从Wikipedia开始:和。第二个链接将为您提供一些可以自己实现的流行算法(如Mersenne Twister)

您还可以使用.Net Reflector反编译System.Random,并将给定的算法与.Net中本机实现的算法进行比较


另外,D.Knuth有一章是关于随机数及其生成的。

正如其他人所评论的,你真的想依靠框架功能来实现这一点。如果这是出于学术目的或纯粹出于兴趣,那么有许多RNG算法很容易实现。一种是带进位乘法(MWC)算法,它可以很容易地在C#中实现:

公共类RNG
{
//种子
静态uint m_w=362436069;/*不得为零*/
静态uint m_z=521288629;/*不得为零*/
public int NextRandom()
{
m_z=36969*(m_z&65535)+(m_z>>16);
m_w=18000*(m_w&65535)+(m_w>>16);

return(int)((m_z为了简单和快速,它很难打败。它是否能生成良好的分布是另一个问题

C#中的一个例子:

不同的语言和环境使用不同的随机数生成器。正如其他人所指出的,有很多方法可以生成伪随机数


请参阅和其他类似的堆栈溢出问题。

不使用Random()方法生成随机数

    using System;
    public class GenerateRandom
    {
        private int max;
        private int last;
        static void Main(string[] args)
        {
          GenerateRandom rand = new GenerateRandom(10);
             for (int i = 0; i < 25; i++)
             {
                 Console.WriteLine(rand.nextInt());
             }
        }
        // constructor that takes the max int
    public GenerateRandom(int max)
    {
        this.max = max;
        DateTime dt = DateTime.Now;//getting current DataTime
        int ms = dt.Millisecond;//getting date in millisecond
        last = (int) (ms % max);
    }

    // Note that the result can not be bigger then 32749
    public int nextInt()
    {
        last = (last * 32719 + 3) % 32749;//this value is set as per our requirement(i.e, these is not a static value
        return last % max;
    }
    }
使用系统;
公共类生成域
{
私人int max;
私家车;
静态void Main(字符串[]参数)
{
GenerateRandom rand=新GenerateRandom(10);
对于(int i=0;i<25;i++)
{
Console.WriteLine(rand.nextInt());
}
}
//取最大int的构造函数
公共生成器域(int max)
{
this.max=max;
DateTime dt=DateTime.Now;//获取当前数据时间
int ms=dt.millissecond;//以毫秒为单位获取日期
最后=(整数)(毫秒最大百分比);
}
//请注意,结果不能大于32749
public int nextInt()
{
last=(last*32719+3)%32749;//此值根据我们的要求设置(即,这些不是静态值
返回最后%max;
}
}

“我不想使用任何内置类或函数。”为什么不呢?看看RNG是如何工作的。你真的想在这里依赖框架功能。看看BCL随机支持是否在.Net即MSIL中实现?我怀疑它是在本机实现的。是的,它在.Net中实现。它们生成了1个数组(每个随机对象)大约有50多个元素,并用类似于MWC的东西填充它,然后返回此种子数组中两个字段之间的差异,同时更新其字段,因此速度非常快(只需访问托管数组的次数很少)。@chibacity“Random类的当前实现基于Donald E.Knuth的减法随机数生成器算法的修改版本。”并且
RNGCryptoServiceProvider
正在使用本机函数。“它是否生成良好的分布是另一个问题。”“只有在种子播得不好的情况下才会出现这种情况。只要在初始化过程中对种子进行散列,Xorshift将产生一些你所见过的最美妙的噪音。下面是一篇比较LCG和Xorshift+Wang散列的精彩文章。
    using System;
    public class GenerateRandom
    {
        private int max;
        private int last;
        static void Main(string[] args)
        {
          GenerateRandom rand = new GenerateRandom(10);
             for (int i = 0; i < 25; i++)
             {
                 Console.WriteLine(rand.nextInt());
             }
        }
        // constructor that takes the max int
    public GenerateRandom(int max)
    {
        this.max = max;
        DateTime dt = DateTime.Now;//getting current DataTime
        int ms = dt.Millisecond;//getting date in millisecond
        last = (int) (ms % max);
    }

    // Note that the result can not be bigger then 32749
    public int nextInt()
    {
        last = (last * 32719 + 3) % 32749;//this value is set as per our requirement(i.e, these is not a static value
        return last % max;
    }
    }