项目中经常需要模拟些假数据,来做测试。这个随机生成数据的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的更多相关文章

  1. Mini projects #5 ---- Memory

    课程全名:An Introduction to Interactive Programming in Python,来自 Rice University 授课教授:Joe Warren, Scott ...

  2. 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 ...

  3. 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 ...

  4. [LeetCode] Copy List with Random Pointer 拷贝带有随机指针的链表

    A linked list is given such that each node contains an additional random pointer which could point t ...

  5. (转)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 ...

  6. python-Day5-深入正则表达式--冒泡排序-时间复杂度 --常用模块学习:自定义模块--random模块:随机验证码--time & datetime模块

    正则表达式   语法:             mport re #导入模块名 p = re.compile("^[0-9]") #生成要匹配的正则对象 , ^代表从开头匹配,[0 ...

  7. NPOI使用教程附Helper

    1 NPOI简介 1.1 NPOI是什么 NPOI是POI的.NET版本,POI是一套用Java写成的库,我们在开发中经常用到导入导出表格.文档的情况,NPOI能够帮助我们在没有安装微软Office的 ...

  8. 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 ...

  9. [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 ...

随机推荐

  1. Linux环境中Openfire安装指南

    Linux环境中Openfire安装指南 安装环境: 安装软件:Openfire 4_1_0 http://download.igniterealtime.org/openfire/openfire_ ...

  2. [Q&A] VS 连接 SQLServer 时未找到或无法访问服务器

    异常信息: 参考解决方法: 1:检查下面两处服务器名是否一致并正确 (VS上) (SQL Server 2008 R2上) 2:VS 在下图位置处对实例名称的设定对于该异常无影响 3:确认在服务中启动 ...

  3. mvc实现上传视频预览

    上个项目中用到了上传视频,本来打算用百度的webuploader做的,但是webuploader可能有个毛病就是不能上传太大的东西. 于是乎,只能换个方法做了啊,看了半天最终决定用传统的uploade ...

  4. c#动态加载卸载DLL的方法

    这篇文章介绍了c#动态加载卸载DLL的方法,有需要的朋友可以参考一下 c#中通过反射可以方便的动态加载dll程序集,但是如果你需要对dll进行更新,却发现.net类库没有提供卸载dll程序集的方法.在 ...

  5. Data type confusion: what is an int(11)?

    http://everythingmysql.ning.com/profiles/blogs/data-type-confusion-what-is-an Over and over I see cu ...

  6. Laravel安装方法 (windows)

    Laravel安装方法(windows) 安装PHP 下载PHP7 http://windows.php.net/download#php-7.0 进入上述网站下载PHP7 选择zip包解压安装 配置 ...

  7. kafka-0.10.0官网翻译(一)入门指南

    1.1 IntroductionKafka is a distributed streaming platform. What exactly does that mean?kafka是一个分布式的流 ...

  8. [转载]T-SQL(MSSQL)语句查询执行顺序

    注意:笔者经过实验和查阅资料,已在原作基础上做了部分更改.更改不代表原作观点,查看原作请点击下方链接. 原文出处: 作者:张龙豪 链接:http://www.cnblogs.com/knowledge ...

  9. mysql存储过程详解

    mysql存储过程详解 1.      存储过程简介   我们常用的操作数据库语言SQL语句在执行的时候需要要先编译,然后执行,而存储过程(Stored Procedure)是一组为了完成特定功能的S ...

  10. AFNetworking的理解

    AFNetworking的理解 使用方法 1. 新建的工程中导入AFNetworking3.0中的(AFNetworking 和UIKit+AFNetworking两个文件夹) 2. 在用到AFNet ...