如何随机生成不同长度的字符串. [英] How to randomly generate different variable length string .the srting be unique.?
问题描述
我创建了一个用于自动随机生成字符串的函数
我在下面给出的功能:
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.........
推荐答案
要随机化字符串的长度,请创建一个1到50的随机数.为此,您可以使用随机 [
To randomize the length of the string, you create a random number from 1 to 50. And to do that, you use Random[^] class. Please see example below.
System.Random RandNum = new System.Random();
int MyRandomNumber = RandNum.Next(1, 51);
这篇文章将为您提供更好的方法.为了生成随机长度的字符串,长度" i/p参数应由"Random"类实例提供,其最大值为50.
希望对您有所帮助!
This article will give you a better approach. Further to generate random length string, the "length" i/p parameter should be provided by a "Random" class instance with max value as say 50.
Hope this helps!
您可以像这样使用
这将生成一个随机长度小于您在Length变量中指定的长度的字符串.
You can use like this
This will generate a string which has a random length less than you have given in Length variable.
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++;
}
谢谢.
这篇关于如何随机生成不同长度的字符串.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!