From 14336fd481112a9876b111c428b5a065c0eb90db Mon Sep 17 00:00:00 2001 From: yxrwendao Date: Wed, 31 Aug 2022 02:25:44 +0800 Subject: [PATCH] =?UTF-8?q?20220831=20=E6=B7=BB=E5=8A=A0=E4=BA=86Rondom?= =?UTF-8?q?=E7=9A=84=E6=89=A9=E5=B1=95=E7=B1=BBRandomEx.cs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/IFoxCAD.Basal/RandomEx.cs | 227 ++++++++++++++++++++++++++++++++++ 1 file changed, 227 insertions(+) create mode 100644 src/IFoxCAD.Basal/RandomEx.cs diff --git a/src/IFoxCAD.Basal/RandomEx.cs b/src/IFoxCAD.Basal/RandomEx.cs new file mode 100644 index 0000000..b29eeb4 --- /dev/null +++ b/src/IFoxCAD.Basal/RandomEx.cs @@ -0,0 +1,227 @@ +/* +*┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━模块信息━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ +*┃ 作 者:YxrWendao +*┃ 创建时间:2022/8/30 22:49:30 +*┃ 模块描述:随机数生成器 +*┃ 使用范围:通用 +*┃ 说 明:本模块中除GetRandom与NextColor方法是IFoxCAD原有的以外,其他方法均通过网络收集整理而来。 +*┃ 代码版本:1.0 +*┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛ +*/ +namespace IFoxCAD.Basal +{ + /* GetRandom与NextColor方法没有在IFoxCAD找到,所以全部整理到此模块,如有重复,请知情者自行删除。 + */ + /// + /// 随机值扩展类 + /// + public static class RandomEx + { + /// + /// 生成一个指定范围的浮点数值 + /// + /// 一个随机值产生器 + /// 范围最小浮点数值 + /// 范围最大浮点数值 + /// + public static double NextDouble(Random ran, double minValue, double maxValue) + { + //https://www.cnblogs.com/qingheshiguang/p/15806915.html + return ran.NextDouble() * (maxValue - minValue) + minValue; + } + /// + /// 生成一个指定范围的浮点数值 + /// + /// 范围最小浮点数值 + /// 范围最大浮点数值 + /// + public static double NextDouble(double minValue, double maxValue) + { + //https://www.cnblogs.com/qingheshiguang/p/15806915.html + return NextDouble(GetRandom(), minValue, maxValue); + } + /// + /// 生成一个布尔随机数 + /// + /// + public static bool NextBool() + { + //https://www.cnblogs.com/qingheshiguang/p/15806915.html + return NextBool(GetRandom()); + } + /// + /// 生成一个布尔随机数 + /// + /// + public static bool NextBool(Random ran) + { + //https://www.cnblogs.com/qingheshiguang/p/15806915.html + bool[] arr = { true, false }; + return arr[ran.Next(2)]; + } + /// + /// 生成一个不连续或指定值的随机值 + /// + /// 一个字符串数组 + /// + public static string NextString(string[] arr) + { + //https://www.cnblogs.com/qingheshiguang/p/15806915.html + return NextString(GetRandom(), arr); + } + /// + /// 生成一个不连续或指定值的随机值 + /// + /// 一个随机值产生器 + /// 一个字符串数组 + /// + public static string NextString(Random ran, string[] arr) + { + //https://www.cnblogs.com/qingheshiguang/p/15806915.html + if (ran is null) + { + ran = GetRandom(); + } + int n = ran.Next(arr.Length - 1); + return arr[n]; + } + /// + /// 生成一个不连续或指定值的随机值 + /// + /// 一个双精度值数组 + /// + public static double NextDouble(double[] arr) + { + //https://www.cnblogs.com/qingheshiguang/p/15806915.html + return NextDouble(GetRandom(), arr); + } + /// + /// 生成不连续或指定值的随机值 + /// + /// 一个随机值产生器 + /// 一个双精度值数组 + /// + public static double NextDouble(Random ran, double[] arr) + { + //https://www.cnblogs.com/qingheshiguang/p/15806915.html + if (ran is null) + { + ran = GetRandom(); + } + int n = ran.Next(arr.Length - 1); + return arr[n]; + } + /// + /// 生成指定范围内的整数 + /// + /// 范围最大整数值 + /// + public static int NextInt(int max) + { + return NextInt(GetRandom(), max); + } + /// + /// 生成指定范围内的整数 + /// + /// 一个随机值产生器 + /// 范围最大整数值 + /// + public static int NextInt(Random ran, int max) + { + if (ran is null) + { + ran = GetRandom(); + } + return ran.Next(max); + } + /// + /// 生成指定范围内的整数 + /// + /// 范围的最小整数 + /// 范围的最大整数 + /// 返回一个介于之间的整数 + public static int NextInt(int min, int max) + { + return NextInt(GetRandom(), min, max); + } + /// + /// 生成指定范围内的整数 + /// + /// 一个随机值产生器 + /// 范围的最小整数 + /// 范围的最大整数 + /// 返回一个介于之间的整数 + public static int NextInt(Random ran, int min, int max) + { + if (ran is null) + { + ran = GetRandom(); + } + return ran.Next(min, max); + } + + /// + /// 生成一个随机颜色 + /// + /// 返回一个System.Drawing.Color + public static System.Drawing.Color NextColor() + { + //来源于:IFoxCAD.Utility类 + return NextColor(GetRandom()); + } + /// + /// 生成一个随机颜色 + /// + /// + public static System.Drawing.Color NextColor(Random ran) + { + //来源于:IFoxCAD.Utility类 + if (ran is null) + { + ran = GetRandom(); + } + int R = ran.Next(255); + int G = ran.Next(255); + int B = ran.Next(255); + B = (R + G > 400) ? R + G - 400 : B;//0 : 380 - R - G; + B = (B > 255) ? 255 : B; + return System.Drawing.Color.FromArgb(R, G, B); + } + + /// + /// 带有随机种子的随机数 + /// 为什么这样写随机种子呢 + /// + /// + public static Random GetRandom() + { + //来源于:IFoxCAD.Utility类 + var tick = DateTime.Now.Ticks; + /* + * 知识准备: + * | 高位64位 | 低位32位 | + * Convert.ToString(int.MaxValue, 2)输出二进制 "1111111111111111111111111111111" 31个;最高位是符号位,所以少1位 + * Convert.ToString(long.MaxValue,2)输出二进制,刚好长一倍 "11111111111111111111111111111111 1111111111111111111111111111111" 63个;最高位是符号位,所以少1位 + * Convert.ToString(0xffffffffL, 2)int.MaxValue再按位多1 "1 1111111111111111111111111111111" 32个;前面的0不会打印出来 + * + * Convert.ToString(long.MaxValue>>32, 2)相当于平移高位的到低位范围,也就是上面少打印的二进制 + * 验证右移是不是高位保留,答案是 + * var a = Convert.ToInt64("101111111111111111111111111111111111111111111111111111111111111", 2); + * Convert.ToString(a >> 32,2); + * + * 解释代码: + * 0x01: + * (int)(long.MaxValue & 0xffffffffL) | (int)(long.MaxValue >> 32); + * Convert.ToString(long.MaxValue & 0xffffffffL, 2)//去掉高位:"11111111111111111111111111111111" 32个,再强转int + * 按位与&是保证符号位肯定是1,其他尽可能为0,高位被去掉只是MaxValue&0的原因,强转才是去掉高位..."尽可能"一词带来第一次随机性 + * 0x02: + * Convert.ToString((long.MaxValue >> 32), 2) //去掉低位: "1111111111111111111111111111111" 31个,再强转int + * 按位或|是尽可能为1..."尽可能"一词带来第二次随机性 + * + */ + + var tickSeeds = (int)(tick & 0xffffffffL) | (int)(tick >> 32); + return new Random(tickSeeds); + } + } +} -- Gitee