C#生成随机字符串
//<summary>
///得到随机字符.
///</summary>
///<param name="intLength">Length of the int.</param>
///<param name="booNumber">if set to <c>true</c> [boo number].</param>
///<param name="booSign">if set to <c>true</c> [boo sign].</param>
///<param name="booSmallword">if set to <c>true</c> [boo smallword].</param>
///<param name="booBigword">if set to <c>true</c> [boo bigword].</param>
///<returns></returns>
public string getRandomizer(int intLength, bool booNumber, bool booSign, bool booSmallword, bool booBigword)
{
//定义
Random ranA = new Random();
int intResultRound = 0;
int intA = 0;
string strB = "";
while (intResultRound < intLength)
{
//生成随机数A,表示生成类型
//1=数字,2=符号,3=小写字母,4=大写字母
intA = ranA.Next(1, 5);
//如果随机数A=1,则运行生成数字
//生成随机数A,范围在0-10
//把随机数A,转成字符
//生成完,位数+1,字符串累加,结束本次循环
if (intA == 1 && booNumber)
{
intA = ranA.Next(0, 10);
strB = intA.ToString() + strB;
intResultRound = intResultRound + 1;
continue;
}
//如果随机数A=2,则运行生成符号
//生成随机数A,表示生成值域
//1:33-47值域,2:58-64值域,3:91-96值域,4:123-126值域
if (intA == 2 && booSign == true)
{
intA = ranA.Next(1, 5);
//如果A=1
//生成随机数A,33-47的Ascii码
//把随机数A,转成字符
//生成完,位数+1,字符串累加,结束本次循环
if (intA == 1)
{
intA = ranA.Next(33, 48);
strB = ((char)intA).ToString() + strB;
intResultRound = intResultRound + 1;
continue;
}
//如果A=2
//生成随机数A,58-64的Ascii码
//把随机数A,转成字符
//生成完,位数+1,字符串累加,结束本次循环
if (intA == 2)
{
intA = ranA.Next(58, 65);
strB = ((char)intA).ToString() + strB;
intResultRound = intResultRound + 1;
continue;
}
//如果A=3
//生成随机数A,91-96的Ascii码
//把随机数A,转成字符
//生成完,位数+1,字符串累加,结束本次循环
if (intA == 3)
{
intA = ranA.Next(91, 97);
strB = ((char)intA).ToString() + strB;
intResultRound = intResultRound + 1;
continue;
}
//如果A=4
//生成随机数A,123-126的Ascii码
//把随机数A,转成字符
//生成完,位数+1,字符串累加,结束本次循环
if (intA == 4)
{
intA = ranA.Next(123, 127);
strB = ((char)intA).ToString() + strB;
intResultRound = intResultRound + 1;
continue;
}
}
//如果随机数A=3,则运行生成小写字母
//生成随机数A,范围在97-122
//把随机数A,转成字符
//生成完,位数+1,字符串累加,结束本次循环
if (intA == 3 && booSmallword == true)
{
intA = ranA.Next(97, 123);
strB = ((char)intA).ToString() + strB;
intResultRound = intResultRound + 1;
continue;
}
//如果随机数A=4,则运行生成大写字母
//生成随机数A,范围在65-90
//把随机数A,转成字符
//生成完,位数+1,字符串累加,结束本次循环
if (intA == 4 && booBigword == true)
{
intA = ranA.Next(65, 89);
strB = ((char)intA).ToString() + strB;
intResultRound = intResultRound + 1;
continue;
}
}
return strB;
}
C#生成随机字符串的更多相关文章
- .net生成随机字符串
生成随机字符串的工具类: /// <summary> /// 随机字符串工具类 /// </summary> public class RandomTools { /// &l ...
- PHP 生成随机字符串与唯一字符串
说明:生成随机字符串用到的方法有 mt_rand() 生成唯一字符串用到的方法有 md5(),uniqid(),microtime() 代码: <?php /* * 生成随机字符串 * @par ...
- PHP生成随机字符串包括大小写字母
PHP生成随机字符串包括大小写字母,这里介绍两种方法: 第一种:利用字符串函数操作 <?php /** *@blog <www.phpddt.com> */ function cre ...
- 生成随机字符串(UUID方法)
这是另一种用UUID生成随机字符串的方法. public class RandomGenerator{ private int length; public void setLength(int le ...
- SQL生成随机字符串
1.SQLserve生成随机字符串 SELECT replace(newid(), '-', '')
- php生成随机字符串可指定纯数字、纯字母或者混合的
php 生成随机字符串 可以指定是纯数字 还是纯字母 或者混合的. 可以指定长度的. function rand_zifu($what,$number){ $string=''; for($i = 1 ...
- JS生成随机字符串的多种方法
这篇文章主要介绍了JS生成随机字符串的方法,需要的朋友可以参考下 下面的一段代码,整理电脑时,记录备查. <script language="javascript"> ...
- PHP生成随机字符串与唯一字符串
代码如下: <?php /* * 生成随机字符串 * @param int $length 生成随机字符串的长度 * @param string $char 组成随机字符串的字符串 * @ret ...
- php 生成随机字符串,数字,大写字母,小写字母,特殊字符可以随意组合
* 生成随机字符串* @param int $length 要生成的随机字符串长度* @param string $type 随机码类型:0,数字+大小写字母:1,数字:2, ...
- PHP中生成随机字符串,数字+大小写字母随机组合
简单的生成随机字符串: /* * 生成随机字符串 * * $length 字符串长度 */ function random_str($length) { // 密码字符集,可任意添加你需要的字符 $c ...
随机推荐
- 剑指Offer37 二叉树深度与平衡二叉树判断
/************************************************************************* > File Name: 37_TreeDe ...
- 剑指Offer39 数组中寻找和为sum的两个数字
/************************************************************************* > File Name: 39_TwoNum ...
- 如果AlertView输入框为空,则禁止点击确定按钮
//UIAlertView的代理方法(创建UIAlertView之后,copy此代理方法即可) - (BOOL)alertViewShouldEnableFirstOtherButton:(UIAle ...
- 【转】对于移动APP测试的一个小技巧
目标:目前越来越多的应用要支持移动设备,html5的推出,方便了页面对移动app的支持,那么我们该如何有效的去测试同时支持app和web的代码?web的测试可以使用浏览器的一些工具来辅助测试,比如ff ...
- c#接口相关
public interface I接口名 { void SaiHi(); void Hello(string msg); //以上是方法 string Name { get; set; } //以上 ...
- WPF视频教程系列笔记
视频二:XAML基础 1.顶级元素 <Window></Window>,<Page></Page>,<Application></Ap ...
- HTML5 Web SQL Database 数据库的使用方法【图文说明】
页面代码: <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" c ...
- asp.net中URL参数传值中文乱码的三种解决办法
在做Asp.Net开发的时候,参数传递中文时,经常会遇到页面乱码的问题,下面是在网上收集的相关资料,请大家参考: 解决的方法一般有3种: 1.设置web.config文件 <system.web ...
- [转]GCC参数详解
[介绍] gcc and g++分别是gnu的c & c++编译器 gcc/g++在执行编译工作的时候,总共需要4步 1.预处理,生成.i的文件[预处理器cpp] 2.将预处理后的文件不转换成 ...
- Objective-C 【autorelease基本使用】
------------------------------------------- NSString中的内存管理问题 由于autoreleasepool的存在,对于内存管理就会很复杂,retain ...