c# 扩展方法奇思妙用基础篇四:Random 扩展
来源:互联网 发布:计算访问消耗数据流量 编辑:程序博客网 时间:2024/04/28 04:10
在 .Net 中,我们一般使用 Random 类来生成随机数,它仅提供了以下几个基本方法供我们使用:
名称说明 Next()返回非负随机数。 Next(Int32)返回一个小于所指定最大值的非负随机数。 Next(Int32, Int32)返回一个指定范围内的随机数。 NextBytes用随机数填充指定字节数组的元素。 NextDouble返回一个介于 0.0 和 1.0 之间的随机数。如果想生成一些特殊的随机数,如枚举、日期等,则要再进行几步处理。本文将会扩展 Random 类,让随机更方便。
Random 常用扩展
布尔:NextBool
随机返回 true 或 false,这个实现比较简单:
public static bool NextBool(this Random random){ return random.NextDouble() > 0.5;}
调用示例:
bool b = random.NextBool();
枚举: NextEnum
public static T NextEnum<T>(this Random random) where T: struct{ Type type = typeof(T); if (type.IsEnum == false) throw new InvalidOperationException(); var array = Enum.GetValues(type); var index = random.Next(array.GetLowerBound(0), array.GetUpperBound(0) + 1); return (T)array.GetValue(index);}
调用示例:
enum Shape { Ellipse, Rectangle, Triangle }Shape shape = random.NextEnum<Shape>();
NextBytes
系统提供的 NextBytes 方法,要事先生成一个 byte 数组,不太方便。扩展一下,输入长度返回数组:
public static byte[] NextBytes(this Random random, int length){ var data = new byte[length]; random.NextBytes(data); return data;}
调用示例:
byte[] data = random.NextBytes(128);
NextUInt16、NextInt16、NextFloat…
有了 NextBytes 扩展,配合 BitConverter 类,我们可以扩展出很多:
public static UInt16 NextUInt16(this Random random){ return BitConverter.ToUInt16(random.NextBytes(2), 0);}public static Int16 NextInt16(this Random random){ return BitConverter.ToInt16(random.NextBytes(2), 0);}public static float NextFloat(this Random random){ return BitConverter.ToSingle(random.NextBytes(4), 0);}
时间日期:NextDateTime
public static DateTime NextDateTime(this Random random, DateTime minValue, DateTime maxValue){ var ticks = minValue.Ticks + (long)((maxValue.Ticks - minValue.Ticks) * random.NextDouble()); return new DateTime(ticks);}public static DateTime NextDateTime(this Random random){ return NextDateTime(random, DateTime.MinValue, DateTime.MaxValue);}
调用示例:
DateTime d = random.NextDateTime(new DateTime(2000, 1, 1), new DateTime(2010, 12, 31));
Random 特定扩展
开发中,面对的问题不同,可能需要不同的扩展,以下只提供几种扩展的思路,如需要,请自行实现:
字符串
- 随机密码,可能需要正则表达式限制,如:
var password = random.NextString(@"\d{6,8}");var passwordSalt = random.NextString(@"[0-9A-F]{16}");
- 随机国家、随机省、城市、地区名称,可能需要提供候选值,如:
var country = random.NextCity("北京", "上海", "广州");
- 随机人名,一定不要生成乱码,要像名字,最好有百家姓和常用人名的数据。
这几个实现起来有难度,有兴趣的朋友可以试下。
验证码:
- 随机颜色(Color):RGB
- 随机字体(Font)
- 随机位置(Point)
- 随机旋转角度
…
(本文中的代码仅供参考,如对随机有更专业的要求,可能不太适合。)
《c#扩展方法奇思妙用》系统文章从 2009 年 08 月开始写起,到现在一共有了 18 篇,欢迎阅读:基础篇:中文处理、string 常用扩展、byte 常用扩展、Random 扩展高级篇:改进 Scottgu 的 "In" 扩展、Aggregate扩展其改进、Enumerable.Cast<T>应用、对扩展进行分组管理、ToString(string format) 扩展、WinForm 控件选择器、树”通用遍历器、Type类扩展变态篇:由Fibonacci数列引出“委托扩展”及“递推递归委托”、封装 if/else、swith/case及while、switch/case 组扩展、string 的翻身革命性能篇:扩展方法性能初测MVC篇:巧用扩展方法优先级,美化所有页面TextBoxFor文本框
- c# 扩展方法奇思妙用基础篇四:Random 扩展
- c# 扩展方法奇思妙用基础:WhereIf 扩展
- c# 扩展方法奇思妙用基础:string 常用扩展
- c# 扩展方法奇思妙用基础:IsBetween 通用扩展
- c#扩展方法奇思妙用高级篇四:对扩展进行分组管理
- c#扩展方法奇思妙用基础篇二:string 常用扩展
- c#扩展方法奇思妙用基础篇三:byte 常用扩展
- c#扩展方法奇思妙用基础篇五DictionaryTKey, TValue 扩展
- c# 扩展方法奇思妙用基础篇九:Expression 扩展
- c# 扩展方法奇思妙用基础篇八:Distinct 扩展
- c# 扩展方法奇思妙用
- c# 扩展方法奇思妙用
- C# 扩展方法奇思妙用
- c#扩展方法奇思妙用变态篇四:string 的翻身革命
- c#扩展方法奇思妙用基础篇一:中文处理
- c# 扩展方法奇思妙用基础:Dictionary<TKey, TValue> 扩展
- c#扩展方法奇思妙用性能篇一:扩展方法性能初测
- c#扩展方法奇思妙用高级篇一:改进 Scottgu 的 "In" 扩展
- python challenge 游戏闯关 学习1
- Combres库 学习小结以及部分源码分析
- android meta-data 使用详解
- Eclipse快捷键大全
- 第04章 异常处理 05 异常的捕获和处理 finally语句
- c# 扩展方法奇思妙用基础篇四:Random 扩展
- 个人作品——类windows自带的计算器
- COFF 文件的格式
- HOG Detector ~ Training OLTbinaries
- X5's Audit Log Architecture in Web Application
- DSP优化心得
- delete this 需要注意呀
- 数据结构面试之二——双向链表表、循环链表、有序链表的常见操作
- jQuery调用ASPX返回json