问题描述
我创建了一个用于自动随机生成字符串的函数
我在下面给出的功能:
Hi i created one function for automatic random generation of string
The function i geiven below:
public static string createRandomString(int Length)
{
string _allowedChars = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ";
Random randNum = new Random();
char[] chars = new char[Length];
int allowedCharCount = _allowedChars.Length;
for (int i = 0; i < Length; i++)
{
chars[i] = _allowedChars[(int)((_allowedChars.Length) * randNum.NextDouble())];
}
return new string(chars);
}
在长度上,我正在传递varchar长度.即varchar(50)表示我正在传递50长度.它随机生成字符串需要50个全长
它最多生成50个字符的字符串
但是我想要可变长度的字符.也就是说,如果我给定长度50,它想在50内产生类似不同长度的字符串..不超过50
那是
例如;
addncvu
axccdfgghh
wdedglikdfdf
adsd
asdfeddfgdfgdfsggggfgfd
advefcvv
adfsadfafccffgbbb
像这样我想改变字符串的长度..如何生成它..如何对我已经编写的函数进行mdodify ..
有人知道告诉我.........
here in length i am passing the varchar length..ie varchar(50) mean i am passing lenght 50.it randomly generate string takes 50 whole length
it generate string upto 50 characters
but i want variable lenght characters.that is if i gave length 50 it want to produce string like different length within 50 ..not exceeds 50
that is
eg;
addncvu
axccdfgghh
wdedglikdfdf
adsd
asdfeddfgdfgdfsggggfgfd
advefcvv
adfsadfafccffgbbb
like this i want to vary the length of string..how to generate it..how to mdodify the function i have written already..
anybody know tell me.........
推荐答案
System.Random RandNum = new System.Random();
int MyRandomNumber = RandNum.Next(1, 51);
public static string createRandomString(int Length)
{
string _allowedChars = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ";
Random randNum = new Random();
char[] chars = new char[Length];
int allowedCharCount = _allowedChars.Length;
for (int i = 0; i < randNum.NextDouble(1,Length); i++)
{
chars[i] = _allowedChars[(int)((_allowedChars.Length) * randNum.NextDouble())];
}
return new string(chars);
}
您还可以使用此代码生成随机字符串
You can Also Use this code to generate Random String
String RndString;
Int Zero, Nine, A, Z, Count, RandNum;
Random randNum;
Random oRandom = New Random(System.DateTime.Now.Millisecond);
Zero = Asc("0");
Z = Asc("Z");
While (Count < randNum.NextDouble(1,Length))
{
RandNum = oRandom.Next(Zero, Z);
RndString = (char)RandNum;
Count++;
}
谢谢.
这篇关于如何随机生成不同长度的字符串.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!