概述
using System;
using System.Collections.Generic;
using System.Text;
namespace CreateRandomNo
{
class RandomCreate
{
/// <summary>
/// 生成数字随机数
/// </summary>
/// <param name="numCount"></param>
/// <returns></returns>
public static string GetNumPwd(int numCount)
{
string allNumers = "0123456789";
StringBuilder sb = new StringBuilder();
for (int i = 0; i < numCount; i++)
{
sb.Append(allNumers[new Random(Guid.NewGuid().GetHashCode()).Next(0, allNumers.Length - 1)]);
}
return sb.ToString();
}
/// <summary>
/// 生成大写字母随机数
/// </summary>
/// <param name="abcCount"></param>
public static string GetABCPwd(int abcCount)
{
string allWords = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
StringBuilder sb = new StringBuilder();
for (int i = 0; i < abcCount; i++)
{
sb.Append(allWords[new Random(Guid.NewGuid().GetHashCode()).Next(0, allWords.Length - 1)]);
}
return sb.ToString();
}
/// <summary>
/// 生成小写字母随机数
/// </summary>
/// <param name="abcCount"></param>
public static string GetabcPwd(int abcCount)
{
string allWords = "abcdefghijklmnopqrstuvwxyz";
StringBuilder sb = new StringBuilder();
for (int i = 0; i < abcCount; i++)
{
sb.Append(allWords[new Random(Guid.NewGuid().GetHashCode()).Next(0, allWords.Length - 1)]);
}
return sb.ToString();
}
/// <summary>
/// 生成混合随机数
/// </summary>
/// <param name="allCount"></param>
/// <returns></returns>
public static string GetMixPwd(int allCount)//生成混合随机数
{
string allNumWord = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
StringBuilder sb = new StringBuilder();
for (int i = 0; i < allCount; i++)
{
sb.Append(allNumWord[new Random(Guid.NewGuid().GetHashCode()).Next(0, allNumWord.Length - 1)]);
}
return sb.ToString();
}
/// <summary>
/// 自己控制输入的符号和随机生成的位数
/// </summary>
/// <param name="refValue">输入的符号</param>
/// <param name="refRandomCount">随机生成的位数</param>
/// <returns></returns>
public static string GetRandomNo(string refValue,int refRandomCount)
{
string value = refValue;
int randomCount = refRandomCount;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < randomCount; i++)
{
sb.Append(value[new Random(Guid.NewGuid().GetHashCode()).Next(0, value.Length - 1)]);
}
return sb.ToString();
}
}
}
后一个方法是自己添加上的用于用户控制显示的符号如[]-$#都可以的,其实对于上述的方法还可以把它们全部合并到一个方法中,但是最近受设计模式的影响,功能单一还是比较好些.这样以后修改起来也比较方便.
最后
以上就是温婉鸭子为你收集整理的C#生成随机数的全部内容,希望文章能够帮你解决C#生成随机数所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复