首页 > 编程语言 >用reflector看到C#Random类的实现

用reflector看到C#Random类的实现

时间:2023-04-17 18:33:02浏览次数:30  
标签:reflector C# inext Random maxValue int num inextp SeedArray

[Serializable, ComVisible(true)]
public class Random
{
// Fields
private int inext;
private int inextp;
private const int MBIG = 0x7fffffff;
private const int MSEED = 0x9a4ec86;
private const int MZ = 0;
private int[] SeedArray;

// Methods
public Random() : this(Environment.TickCount)
{
}

public Random(int Seed)
{
this.SeedArray = new int[0x38];
int num2 = 0x9a4ec86 - Math.Abs(Seed);
this.SeedArray[0x37] = num2;
int num3 = 1;
for (int i = 1; i < 0x37; i++)
{
int index = (0x15 * i) % 0x37;
this.SeedArray[index] = num3;
num3 = num2 - num3;
if (num3 < 0)
{
num3 += 0x7fffffff;
}
num2 = this.SeedArray[index];
}
for (int j = 1; j < 5; j++)
{
for (int k = 1; k < 0x38; k++)
{
this.SeedArray[k] -= this.SeedArray[1 + ((k + 30) % 0x37)];
if (this.SeedArray[k] < 0)
{
this.SeedArray[k] += 0x7fffffff;
}
}
}
this.inext = 0;
this.inextp = 0x15;
Seed = 1;
}

private double GetSampleForLargeRange()
{
int num = this.InternalSample();
if ((this.InternalSample() % 2) == 0)
{
num = -num;
}
double num2 = num;
num2 += 2147483646.0;
return (num2 / 4294967293);
}

private int InternalSample()
{
int inext = this.inext;
int inextp = this.inextp;
if (++inext >= 0x38)
{
inext = 1;
}
if (++inextp >= 0x38)
{
inextp = 1;
}
int num = this.SeedArray[inext] - this.SeedArray[inextp];
if (num < 0)
{
num += 0x7fffffff;
}
this.SeedArray[inext] = num;
this.inext = inext;
this.inextp = inextp;
return num;
}

public virtual int Next()
{
return this.InternalSample();
}

public virtual int Next(int maxValue)
{
if (maxValue < 0)
{
throw new ArgumentOutOfRangeException("maxValue", string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("ArgumentOutOfRange_MustBePositive"), new object[] { "maxValue" }));
}
return (int) (this.Sample() * maxValue);
}

public virtual int Next(int minValue, int maxValue)
{
if (minValue > maxValue)
{
throw new ArgumentOutOfRangeException("minValue", string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Argument_MinMaxValue"), new object[] { "minValue", "maxValue" }));
}
long num = maxValue - minValue;
if (num <= 0x7fffffffL)
{
return (((int) (this.Sample() * num)) + minValue);
}
return (((int) ((long) (this.GetSampleForLargeRange() * num))) + minValue);
}

public virtual void NextBytes(byte[] buffer)
{
if (buffer == null)
{
throw new ArgumentNullException("buffer");
}
for (int i = 0; i < buffer.Length; i++)
{
buffer[i] = (byte) (this.InternalSample() % 0x100);
}
}

public virtual double NextDouble()
{
return this.Sample();
}

protected virtual double Sample()
{
return (this.InternalSample() * 4.6566128752457969E-10);
}
}

标签:reflector,C#,inext,Random,maxValue,int,num,inextp,SeedArray
From: https://blog.51cto.com/u_16076050/6195837

相关文章

  • docker Ubuntu 安装教程
    启动docker镜像dockerrun-t-i-dubuntu:18.04/bin/bash配置ustc镜像源sed-i's@//.*archive.ubuntu.com@//mirrors.ustc.edu.cn@g'/etc/apt/sources.listsed-i's/security.ubuntu.com/mirrors.ustc.edu.cn/g'/etc/apt/sources.listapt-getcleanap......
  • c# 扩展方法
    由来一个类想要有新的方法,除了简单粗暴的在类中直接添加,当然可以用继承来实现,不过若为扩展一个方法就用继承,这就大材小用了,况且有些类是不能被继承的。于是乎,c#3.0提出了扩展方法,用它来为现有的类型(比如自定义的类)添加方法。如何定义扩展方法a)扩展方法必须在非嵌套(类中类)非......
  • opencv c++ 保存为位深度为1的png
    vector<int>compression_params;compression_params.push_back(IMWRITE_PNG_COMPRESSION);compression_params.push_back(3);compression_params.push_back(IMWRITE_PNG_BILEVEL);compression_params.push_back(1);imwrite("text2.png&......
  • 文本编辑器 实现ctrl+v粘贴图片并上传、word粘贴带图片
    ​ 当前功能基于PHP,其它语言流程大抵相同。大概流程:1.将docx文件上传到服务器中2.使用PHPoffice/PHPword实现将word转换为HTML3.将HTML代码返回并赋值到编辑器中 1编辑器配置修改1.1新增上传wordjson配置在ueditor\php\config.json中新增如下配置:     /*......
  • 编程打卡:C语言趣味编程习题做
    编程打卡:C语言趣味编程习题做数制转换问题描述给定一个M进制的数x,实现对x向任意非M进制的数的转换。设计思路输入M进制的数x,将x转换为十进制数,再将十进制数转换为任意非M进制的数。流程图graphA["开始"]-->B["输入M进制的数x"]-->C["将x转换为十进制数"]-->D["将十进......
  • 2-211-(LeetCode-470) 用 Rand7() 实现 Rand10()
     1.题目 https://leetcode.cn/problems/implement-rand10-using-rand7/submissions/425373186/ 2.解法 classSolutionextendsSolBase{publicintrand10(){inttemp=40;while(temp>=40){temp=(rand7()-1)*7......
  • CF1646E Power Board 题解
    题目链接:https://codeforces.com/contest/1646/problem/E题目大意:有一个\(n\timesm\)的矩阵,其中第\(i\)行第\(j\)列的格子中的数字是\(i^j\)。问:矩阵中存在多少个不同的数?解题思路:可以很明显地发现,第\(1\)行的数字全部都是\(1\),而且在其它行不会出现数值为\(1\)......
  • Docker基础命令及面试问题
    Docker包括三个基本概念:镜像(Image):Docker镜像(Image),就相当于是一个root文件系统。比如官方镜像ubuntu:16.04就包含了完整的一套Ubuntu16.04最小系统的root文件系统。容器(Container):镜像(Image)和容器(Container)的关系,就像是面向对象程序设计中的类和实例一样,镜像是静态的定......
  • Failed to process import candidates for configuration class [springfox.documenta
     org.springframework.beans.factory.BeanDefinitionStoreException:Failedtoprocessimportcandidatesforconfigurationclass[springfox.documentation.swagger2.configuration.Swagger2DocumentationConfiguration];nestedexceptionisjava.lang.IllegalArgumen......
  • 连接MongoDB+Docker安装MongoDB
    一、连接MongoDB工具:studio3T下载:https://studio3t.com/download-thank-you/?OS=win641、无设置密码最终成功页面2、设置了密码后续同1二、安装MongoDB版本:5.0.5参考:https://www.cnblogs.com/cwp-bg/p/10403327.htmlhttps://blog.csdn.net/weixin_4......