C# 随机数问题 分数不是问题

DataBox-MDX 2011-08-30 04:23:28
1、如何随机生成N个随机字母的组合:如N=3, abc、asd、aas、ccs等等
2、如何随机生成M个随机数字的组合:如N=5,12342、21354、21456
分数不是问题。
...全文
243 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
诚朴勇毅 2011-08-30
  • 打赏
  • 举报
回复
顶3楼。。。
  • 打赏
  • 举报
回复
来一段儿,这个网上一搜一大把的

/// <summary>
/// 返回大写字母加数字的随机编码
/// </summary>
/// <param name="CodeLength"></param>
/// <returns></returns>
public static string GetRndCode(int CodeLength)
{
string Vchar = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";

string[] VcArray = Vchar.Split(',');
string RndCode = string.Empty;
int temp = -1; //记录上次随机数值

//采用一个简单的算法以保证生成随机数的不同
Random rand = new Random();
for (int i = 0; i < CodeLength; i++)
{
if (temp != -1)
{
rand = new Random(i * temp * unchecked((int)DateTime.Now.Ticks));
}
int t = rand.Next(35);
//如果出现与上次一样的字符,则重新生成一个
if (temp != -1 && temp == t)
{
return GetRndCode(CodeLength);
}
temp = t;
RndCode += VcArray[t];
}
return RndCode;
}
  • 打赏
  • 举报
回复
留个标记备用
.云哲. 2011-08-30
  • 打赏
  • 举报
回复
唉!晚了完了。没分了。
上面的方案够用了。
phoebuswei 2011-08-30
  • 打赏
  • 举报
回复

private static StringBuilder getRandom( bool isOverlap, int number, int min, int max)
{
var sb = new StringBuilder();
Random rd = new Random(Environment.TickCount + DateTime.Now.Millisecond);
if (isOverlap)
{
for (int i = 0; i < number; i++)
{
sb.Append(Convert.ToChar(rd.Next(min, max)));
}
}
else
{
var Temporary = new List<char>();
for (int i = 0; i < number; i++)
{
while (true)
{
var isindex = Convert.ToChar(rd.Next(min, max));
if (Temporary.Contains(isindex))
{
continue;
}
else
{
Temporary.Add(isindex);
sb.Append(isindex);
break;
}
}
}
}
return sb;
}

例子
getRandom(不重复true,3个,)
max = 123; min = 65;字母
max = 58; min = 48; 数字

szjarvis 2011-08-30
  • 打赏
  • 举报
回复
每天回贴有分得。
就这么拽 2011-08-30
  • 打赏
  • 举报
回复
来学习的
kiss筱魔 2011-08-30
  • 打赏
  • 举报
回复

<script type="text/javascript" language="javascript">function CreateStr()
{
var j=document.getElementById("input2").value;
if(isNaN(j))
{
alert("请输入整数");
}
var tmpCh = "";
var i=1;
for(i=1;i<=j;i++)
{
tmpCh += String.fromCharCode(Math.floor( Math.random() * 26) + "a".charCodeAt(0));
}
alert(tmpCh);
}
function CreateNo()
{

var j=document.getElementById("input3").value;

if(isNaN(j))
{
alert("请输入整数");
}

var s = (Math.floor(Math.random()*10000) % j+1).toString();


for(var i=1;i<j; i++)
{
s = s + (Math.floor(Math.random()*10000) % 10).toString();
}
alert(s);
}


</script>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>

</HEAD>

<BODY>
<input type="input" id="input2" />
<input type="button" onclick="CreateStr()" value="生成随机字母" width="120px" height="28px"/>
<input type="input" id="input3" />
<input type="button" onclick="CreateNo()" value="生成随机数" width="120px" height="28px"/>
</BODY>
</HTML>
qqdd2716 2011-08-30
  • 打赏
  • 举报
回复
/// <summary>
/// 生成随机字符串
/// </summary>
/// <param name="length">目标字符串的长度</param>
/// <param name="useNum">是否包含数字,1=包含,默认为包含</param>
/// <param name="useLow">是否包含小写字母,1=包含,默认为包含</param>
/// <param name="useUpp">是否包含大写字母,1=包含,默认为包含</param>
/// <param name="useSpe">是否包含特殊字符,1=包含,默认为不包含</param>
/// <param name="custom">要包含的自定义字符,直接输入要包含的字符列表</param>
/// <returns>指定长度的随机字符串</returns>
public string GetRnd(int length, bool useNum, bool useLow, bool useUpp, bool useSpe, string custom)
{
byte[] b = new byte[4];
new System.Security.Cryptography.RNGCryptoServiceProvider().GetBytes(b);
Random r = new Random(BitConverter.ToInt32(b, 0));
string s = null, str = custom;

if (useNum == true) { str += "0123456789"; }
if (useLow == true) { str += "abcdefghijklmnopqrstuvwxyz"; }
if (useUpp == true) { str += "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; }
if (useSpe == true) { str += "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"; }

for (int i = 0; i < length; i++)
{
s += str.Substring(r.Next(0, str.Length - 1), 1);
}

return s;
}
LMAOhuaNL 2011-08-30
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 c5662601 的回复:]

C# code
/// <summary>
/// 生成随机数
/// </summary>
/// <param name="pwdLength">参数值</param>
/// <returns></returns>
public static string MakePassword(int PwdLength……
[/Quote]
这方法也不错的
C5662601 2011-08-30
  • 打赏
  • 举报
回复
/// <summary>
/// 生成随机数
/// </summary>
/// <param name="pwdLength">参数值</param>
/// <returns></returns>
public static string MakePassword(int PwdLength)
{
string Tmp = "";
string pwd ="abcdefghkmnpqrstuvwxyz23456789ABCDEFGHKMNPQRSTUVWXYZ";
int MakeRandNum;
Random rnd = new Random();
for(int i=0;i<PwdLength;i++)
{
MakeRandNum = rnd.Next(pwd.Length);
Tmp += pwd[MakeRandNum];
}
return Tmp;
}
子夜__ 2011-08-30
  • 打赏
  • 举报
回复
 StringBuilder sb = new StringBuilder();
string Table = "abcdefghijklmnopqrstuvwxyz";

int[] p = new int[26];//字符索引
int Length = 3 - 1;//字符串的长度
int i = Length;
p[i] = -1;
int a = 0;
while (a++ <= Length) sb.Append("a");
while (i > -1)
{
if (p[i] == 25)
{
sb.Remove(i, 1);
i--;
continue;
}
else
{
p[i] = p[i] + 1;
sb[i] = Table[p[i]];
if (i < Length)
{
i++;
p[i] = -1;
sb.Append(" ");
continue;
}
}
Response.Write(sb.ToString() + "<br/>");
}

或者把值存入ISET里来查
行游客 2011-08-30
  • 打赏
  • 举报
回复

string[] s = new string[26]{"a","b","c","d","e","f","g",...};

Random ran = new Random();
int n = 0;
int m=0;

string nValue = "";
string mValue = "";
int index = 0;
for(int i = 0;i<n;i++)
{
index = ran.Next(0,25);

nValue += s[index];
}

for(int i=0;i<m;i++)
{
index = ran.Next(0,9);
mValue += index.ToString();
}
ruanwei1987 2011-08-30
  • 打赏
  • 举报
回复
不错不错
楼上 代码很细
wangxiaofeiwuqiao 2011-08-30
  • 打赏
  • 举报
回复
字母随机或者:

/// <summary>
/// 字母随机数
/// </summary>
/// <param name="n">生成长度</param>
/// <returns></returns>
public static string RandLetter(int n)
{
char[] arrChar = new char[]{
'a','b','d','c','e','f','g','h','i','j','k','l','m','n','p','r','q','s','t','u','v','w','z','y','x',
'_',
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','Q','P','R','T','S','V','U','W','X','Y','Z'
};
StringBuilder num = new StringBuilder();
Random rnd = new Random(DateTime.Now.Millisecond);
for (int i = 0; i < n; i++)
{
num.Append(arrChar[rnd.Next(0, arrChar.Length)].ToString());
}
return num.ToString();
}
wangxiaofeiwuqiao 2011-08-30
  • 打赏
  • 举报
回复
字母随机或者:

/// <summary>
/// 字母随机数
/// </summary>
/// <param name="n">生成长度</param>
/// <returns></returns>
public static string RandLetter(int n)
{
char[] arrChar = new char[]{
'a','b','d','c','e','f','g','h','i','j','k','l','m','n','p','r','q','s','t','u','v','w','z','y','x',
'_',
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','Q','P','R','T','S','V','U','W','X','Y','Z'
};
StringBuilder num = new StringBuilder();
Random rnd = new Random(DateTime.Now.Millisecond);
for (int i = 0; i < n; i++)
{
num.Append(arrChar[rnd.Next(0, arrChar.Length)].ToString());
}
return num.ToString();
}

xujun5031 2011-08-30
  • 打赏
  • 举报
回复
mark
rascalwm 2011-08-30
  • 打赏
  • 举报
回复
楼上的 方法不通用 为3的时候 楼主要求是 出字母 不是数字 这之中还需要有个 字母和数字的转换
wangxiaofeiwuqiao 2011-08-30
  • 打赏
  • 举报
回复

// 生成随机数字
// <param name="length">生成长度</param>
public static string Number(int Length)
{
return Number(Length, false);
}

/// 生成随机数字
/// <param name="Length">生成长度</param>
/// <param name="Sleep">是否要在生成前将当前线程阻止以避免重复</param>
public static string Number(int Length,bool Sleep)
{
if(Sleep)
System.Threading.Thread.Sleep(3);
string result = "";
System.Random random = new Random();
for (int i = 0; i < Length; i++)
{
result += random.Next(10).ToString();
}
return result;
}


/// <summary>
/// 生成随机纯字母随机数
/// </summary>
/// <param name="IntStr">生成长度</param>
/// <returns></returns>
public static string Str_char(int Length)
{
return Str_char(Length, false);
}

/// <summary>
/// 生成随机纯字母随机数
/// </summary>
/// <param name="Length">生成长度</param>
/// <param name="Sleep">是否要在生成前将当前线程阻止以避免重复</param>
/// <returns></returns>
public static string Str_char(int Length, bool Sleep)
{
if (Sleep) System.Threading.Thread.Sleep(3);
char[] Pattern = new char[] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
string result = "";
int n = Pattern.Length;
System.Random random = new Random(~unchecked((int)DateTime.Now.Ticks));
for (int i = 0; i < Length; i++)
{
int rnd = random.Next(0, n);
result += Pattern[rnd];
}
return result;
}
}

qq290032431 2011-08-30
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 skydemo 的回复:]
笨方法
Random ra =new Random();
string pt="";
int res=0;
switch (pt)
{
case "3":
res= ra.Next(100, 999);
break;
case "5":
res= ra.Next(10000, 99999);
break;
.....
}……
[/Quote]
那字母呢?
加载更多回复(1)

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧