C# random helper class
项目中经常需要模拟些假数据,来做测试。这个随机生成数据的helper类就应用而生:
using System;
using System.Text;
using System.Windows.Media; namespace WpfApplication1.Helper
{
public static class RandomHelper
{
private static Random randomSeed = new Random(); /// <summary>
/// Generates a random string with the given length
/// </summary>
/// <param name="size">Size of the string</param>
/// <param name="lowerCase">If true, generate lowercase string</param>
/// <returns>Random string</returns>
public static string RandomString(int size, bool lowerCase)
{
// StringBuilder is faster than using strings (+=)
StringBuilder RandStr = new StringBuilder(size); // Ascii start position (65 = A / 97 = a)
int Start = (lowerCase) ? 97 : 65; // Add random chars
for (int i = 0; i < size; i++)
RandStr.Append((char)(26 * randomSeed.NextDouble() + Start)); return RandStr.ToString();
} public static int RandomInt(int min, int max)
{
return randomSeed.Next(min, max);
} public static double RandomDouble()
{
return randomSeed.NextDouble();
} public static double RandomNumber(int min, int max, int digits)
{
return Math.Round(randomSeed.Next(min, max - 1) + randomSeed.NextDouble(), digits);
} public static bool RandomBool()
{
return (randomSeed.NextDouble() > 0.5);
} public static DateTime RandomDate()
{
return RandomDate(new DateTime(1900, 1, 1), DateTime.Now);
} public static DateTime RandomDate(DateTime from, DateTime to)
{
TimeSpan range = new TimeSpan(to.Ticks - from.Ticks);
return from + new TimeSpan((long)(range.Ticks * randomSeed.NextDouble()));
} public static Color RandomColor()
{
return Color.FromRgb((byte)randomSeed.Next(255), (byte)randomSeed.Next(255), (byte)randomSeed.Next(255));
} }
}
C# random helper class的更多相关文章
- Mini projects #5 ---- Memory
课程全名:An Introduction to Interactive Programming in Python,来自 Rice University 授课教授:Joe Warren, Scott ...
- Mini-project # 1 - Rock-paper-scissors-___An Introduction to Interactive Programming in Python"RICE"
Mini-project description - Rock-paper-scissors-lizard-Spock Rock-paper-scissors is a hand game that ...
- Game Development Patterns and Best Practices (John P. Doran / Matt Casanova 著)
https://github.com/PacktPublishing/Game-Development-Patterns-and-Best-Practices https://github.com/m ...
- [LeetCode] Copy List with Random Pointer 拷贝带有随机指针的链表
A linked list is given such that each node contains an additional random pointer which could point t ...
- (转)Image Segmentation with Tensorflow using CNNs and Conditional Random Fields
Daniil's blog Machine Learning and Computer Vision artisan. About/ Blog/ Image Segmentation with Ten ...
- python-Day5-深入正则表达式--冒泡排序-时间复杂度 --常用模块学习:自定义模块--random模块:随机验证码--time & datetime模块
正则表达式 语法: mport re #导入模块名 p = re.compile("^[0-9]") #生成要匹配的正则对象 , ^代表从开头匹配,[0 ...
- NPOI使用教程附Helper
1 NPOI简介 1.1 NPOI是什么 NPOI是POI的.NET版本,POI是一套用Java写成的库,我们在开发中经常用到导入导出表格.文档的情况,NPOI能够帮助我们在没有安装微软Office的 ...
- libevent reference Mannual IV --Helper functions and types
FYI: http://www.wangafu.net/~nickm/libevent-book/Ref5_evutil.html Helper functions and types for Lib ...
- [LeetCode] 138. Copy List with Random Pointer 拷贝带有随机指针的链表
A linked list is given such that each node contains an additional random pointer which could point t ...
随机推荐
- Model验证功能
占位符 {0}对应属性 {1}对应minimum {2}对应maximum [StringLength(15, MinimumLength = 6, ErrorMessage = " ...
- 更换Kali源让你更新更快
在2016.1版本kali-linux(也就是kali滚动更新版)更新慢解决办法: (此源为2.0版本)中科大kali滚动更新版源(即kali2.0源) #kali官方源 deb http://htt ...
- Java中随机数的产生方式与原理
查阅随机数相关资料,特做整理 首先说一下java中产生随机数的几种方式 在j2se中我们可以使用Math.random()方法来产生一个随机数,这个产生的随机数是0-1之间的一个double,我们可以 ...
- python基础之条件控制与循环
Python3 条件控制 Python条件语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块. 计算机之所以能做很多自动化的任务,因为它可以自己做条件判断. 比如,输入用户 ...
- mysql基本操作
1.创建表:create table if not exists student(id integer(4) primary key auto_increment,name varchar(10),s ...
- Visual Studio for Mac 简介
2016-12-13 Hutchinson 微软中国MSDN 在 11 月举行的 Connect(); 上,Microsoft 将推出 Visual Studio for Mac 预览版.这是一个激动 ...
- .NET程序员走向高端必读书单汇总
.NET程序员走向高端必读书单汇总 一.知识树 1. 基本能力 1.1 数学 1.2 英语 1.3 语言表达 2. 计算机组织与体系结构 3. 算法与数据结构 4. 操作系统 5. 计算机网络 6. ...
- Android开发3:Intent、Bundle的使用和ListView的应用 、RelativeLayout(相对布局)简述(简单通讯录的实现)
前言 啦啦啦~博主又来骚扰大家啦~大家是不是感觉上次的Android开发博文有点长呢~主要是因为博主也是小白,在做实验的过程中查询了很多很多概念,努力去理解每一个知识点,才完成了最终的实验.还有就是随 ...
- 关于在SharePoint 2013(2010)中Javascript如何实现批量批准的自定义操作功能?
1.概述: SharePoint 2013(包括SharePoint 2010)提供了很方便的,多选的界面,但是很多操作还是不能批量进行,比如:批准的功能.如果您要解决方案不关心代码,那么请直接联系作 ...
- eclipse启动时报错 Could not create the java virtual machine
eclipse启动的时候 报错 这是系统为eclipse分配的内存不足,需要去修改 eclipse.ini文件 eclipse.ini是eclipse 内存分配之类的配置文件 对java虚拟机的 ...