博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c# Random Class usage
阅读量:5276 次
发布时间:2019-06-14

本文共 8601 字,大约阅读时间需要 28 分钟。

Random Usage

sometimes, we hope to generate some random infor to manipulate our data structor. we can use random class to achieve this goal.

the refer link in MSDN,

example source

using System;using System.Text;using System.Windows.Media;namespace MVVM_Template_Project.Auxiliary.Helpers{    ///     /// This class should provide you with plenty of random method to get random things.    /// Benefit: no need to create a `Random rand = new Randow();` everywhere in your code.    ///     public static class Random_Helper    {        private static readonly Random RandomSeed = new Random();                ///         /// Generates a random string with the given length        ///         /// Size of the string        /// If true, generate lowercase string        /// 
Random string
public static string RandomString(int size, bool lowerCase) { // StringBuilder is faster than using strings (+=) var randStr = new StringBuilder(size); // Ascii start position (65 = A / 97 = a) var start = (lowerCase) ? 97 : 65; // Add random chars for (var i = 0; i < size; i++) randStr.Append((char) (26*RandomSeed.NextDouble() + start)); return randStr.ToString(); } /// /// Number between min (inclusive) and max (exclusive) /// /// Inclusive /// Exclusive ///
public static int RandomInt(int min, int max) { return RandomSeed.Next(min, max); } /// /// Double between 0 (inclusive) and 1 (exclusive) /// public static double RandomDouble() { return RandomSeed.NextDouble(); } /// /// Return a random number between min (inc) and max (exc), with 'digits' /// numbers after the decimal point. /// /// Lowest number (inclusive) /// Highest number (exclusive) /// Precision after decimal point public static double RandomNumber(int min, int max, int digits) { var range = max-min; var str_format = "{0:F"+digits+"}"; // To avoid having to get an int and then add a random double ... var rand_dou = Double.Parse(string.Format(str_format, min + range * RandomSeed.NextDouble())); // If for some random reason the rounding of the double is equal to max, we just call // the method again and return the value. // This can *theoretically* get into an infinite loop if it happens every time, but // this scenario is highly unlikely :0 return (rand_dou
/// Random true/false. /// public static bool RandomBool() { return (RandomSeed.NextDouble() > 0.5); } ///
/// Random phone nmuber. Starts with 1234 and has extra 6 random digts. /// public static string RandomPhone() { string mold = "1234-{0}-{1}"; return string.Format(mold, RandomInt(0, 1000).ToString().PadLeft(3, '0'), RandomInt(0, 1000).ToString().PadLeft(3, '0')); } ///
/// Will return a random date between 1/1/1900 and Now. /// public static DateTime RandomDate() { return RandomDate(new DateTime(1900, 1, 1), DateTime.Now); } ///
/// Will return a random date between given dates. /// ///
lower bound date ///
higher bound date ///
public static DateTime RandomDate(DateTime from, DateTime to) { var range = new TimeSpan(to.Ticks - from.Ticks); return from + new TimeSpan((long) (range.Ticks*RandomSeed.NextDouble())); } ///
/// Random color (RGB) /// public static Color RandomColor() { return Color.FromRgb((byte) RandomSeed.Next(255), (byte) RandomSeed.Next(255), (byte) RandomSeed.Next(255)); } ///
/// Random weather. Note that valid values are 0-5 (inclusive), and so the /// default might happen. /// ///
public static string RandomWeather() { int rand = RandomInt(0, 6); string weather; switch (rand) { case 0: weather = "Gloomy day"; break; case 1: weather = "Rainy day"; break; case 2: weather = "Foggy day"; break; case 3: weather = "Sunny day"; break; case 4: weather = "Rainy with a chance of meatballs"; break; default: weather = "I'm stuck in my cubicle coding. No weather for you! Come back, 1 YEAR!"; break; } return weather; } #region Random names private static readonly string[] ListOfMaleFirstNames = { "Verna", "Almeta", "Melvina", "Digna", "Dortha", "Ione", "Sonya", "Shiela", "Shonna", "Tania", "Susanne", "Ellie", "Felice", "Caitlyn", "Bethel", "Kamilah", "Camila", "Stefanie", "Daysi", "Brittaney", "Lavonda", "Janice", "Tiny", "Peg", "Kaila", "Janay", "Inga", "Melissa", "Delila", "Patience", "Delma", "Ressie", "Nenita", "Casimira", "Theda", "Ethel", "Christinia", "Nyla", "Letha", "Lea", "Cindy", "Nancy", "Jazmine", "Vanita", "Larhonda", "Tai", "Charise", "Latoria", "Shanti", "Kyla" }; private static readonly int MaleFirstNamesCount = ListOfMaleFirstNames.Length; private static readonly string[] ListOfFemaleFirstNames = { "Verna", "Almeta", "Melvina", "Digna", "Dortha", "Ione", "Sonya", "Shiela", "Shonna", "Tania", "Susanne", "Ellie", "Felice", "Caitlyn", "Bethel", "Kamilah", "Camila", "Stefanie", "Daysi", "Brittaney", "Lavonda", "Janice", "Tiny", "Peg", "Kaila", "Janay", "Inga", "Melissa", "Delila", "Patience", "Delma", "Ressie", "Nenita", "Casimira", "Theda", "Ethel", "Christinia", "Nyla", "Letha", "Lea", "Cindy", "Nancy", "Jazmine", "Vanita", "Larhonda", "Tai", "Charise", "Latoria", "Shanti", "Kyla" }; private static readonly int FemaleFirstNamesCount = ListOfFemaleFirstNames.Length; private static readonly string[] ListOfLastNames = { "Smith", "Jones", "Williams", "Brown", "Wilson", "Taylor", "Morton", "White", "Martin", "Anderson", "Thompson", "Nguyen", "Thomas", "Walker", "Harris", "Lee", "Ryan", "Robinson", "Kelly", "King", "González", "Rodríguez", "Hernández", "Pérez", "García", "Martín", "Santana", "Díaz", "Suárez", "Sánchez", "Smith", "Brown", "Lee", "Wilson", "Martin", "Patel", "Taylor", "Wong", "Campbell", "Williams", "Kim", "Lee", "Park", "Choi", "Jeong", "Kang", "Cho", "Yoon", "Jang", "Lim" }; private static readonly int LastNamesCount = ListOfLastNames.Length; #endregion ///
/// Return a random name. Might be male/female and with a diverse last name range. /// ///
If true will return male name ///
If true will return female name ///
If both param are true or null, random name. Otherwise, male or female /// name depending on the param that is true
public static string RandomName(bool? aMale=null, bool? aFemale=null) { string first; // Not specified, select at random if ((aMale == null && aFemale == null) || (aMale == true && aFemale == true)) { var gender = RandomBool(); first = (gender) ? ListOfMaleFirstNames[RandomInt(0, MaleFirstNamesCount)] : ListOfFemaleFirstNames[RandomInt(0, FemaleFirstNamesCount)]; } else if (aMale == true) first = ListOfMaleFirstNames[RandomInt(0, MaleFirstNamesCount)]; else first = ListOfFemaleFirstNames[RandomInt(0, FemaleFirstNamesCount)]; string last = ListOfLastNames[RandomInt(0, LastNamesCount)]; return string.Format("{0} {1}", first, last); } }}

转载于:https://www.cnblogs.com/kongshu-612/p/5469713.html

你可能感兴趣的文章
android:scaleType属性
查看>>
shell脚本
查看>>
Upload Image to .NET Core 2.1 API
查看>>
【雷电】源代码分析(二)-- 进入游戏攻击
查看>>
Linux中防火墙centos
查看>>
如何设置映射网络驱动器的具体步骤和方法
查看>>
centos下同时启动多个tomcat
查看>>
Leetcode Balanced Binary Tree
查看>>
[JS]递归对象或数组
查看>>
linux sed命令
查看>>
湖南多校对抗赛(2015.03.28) H SG Value
查看>>
程序存储问题
查看>>
优雅地书写回调——Promise
查看>>
AX 2009 Grid控件下多选行
查看>>
PHP的配置
查看>>
Struts框架----进度1
查看>>
Round B APAC Test 2017
查看>>
MySQL 字符编码问题详细解释
查看>>
Windows 2003全面优化
查看>>
格而知之2:UIView的autoresizingMask属性探究
查看>>