string str = "test string"; Random rnd = new Random(Environment.TickCount); int a = rnd.Next(0, str.Length - 1); int b = a; while (b == a) b = rnd.Next(0, str.Length - 1); string result = str.Substring(a, 1) + str.Substring(b, 1); Console.WriteLine(result);
相当于登录时候的验证码,对不?string str = "的一是在不了有和人这中大为上个国我以要他时来用们..."; char[] chastr = str.ToCharArray(); string code = ""; Random rd = new Random(); int i; for (i = 0; i < num; i++) { code += str.Substring(rd.Next(0, str.Length), 1); } return code;
Random rnd=new Random(DateTime.Now.Millisecond); int a = rnd.Next(0, 数组.Length - 1);
如果.net 3.5 vs2008 用Linqstring str = "就是这些测试字符串"; Random rnd = new Random(Environment.TickCount); string result = new string(str.ToCharArray().OrderBy(c=>rnd.Next()).Take(2).ToArray());
string[] strArr = { "aaa", "bbb", "ccc", "..." }; Random rnd = new Random(); int i1 = rnd.Next(0, strArr.Length); int i2 = rnd.Next(0, strArr.Length); for (; i2 == i1; ) { i2 = rnd.Next(0, strArr.Length); } 即strArr[i1]+strArr[i2]是你所要的二个随机数。
string[] strArr = { "aaa", "bbb", "ccc", "..." }; Random rnd = new Random(); int i1 = rnd.Next(0, strArr.Length - 1); int i2 = rnd.Next(0, strArr.Length - 1); for (; i2 == i1; ) { i2 = rnd.Next(0, strArr.Length); } 即strArr[i1]+strArr[i2]是你所要的二个随机数。刚才忘记数组未减1,现在改正
ArrayList al = new ArrayList();
//生成数据
int dataLength = 10;
for (int i = 0; i < dataLength; i++)
{
al.Add(i);
} Random rd = new Random();
for (int j = 0; j < dataLength; j++)
{
int randomIndex = rd.Next(0, al.Count);
Console.Write(al[randomIndex].ToString() + " ");
al.RemoveAt(randomIndex);
}
String str = "abcdefghij";
Random rd = new Random((Int32)DateTime.Now.Ticks); Int32 i = rd.Next(0,str.Length); Int32 j = rd.Next(1, str.Length - i); String s1 = str.Substring(i, j);
i = rd.Next(0, str.Length); j = rd.Next(1, str.Length - i); String s2 = str.Substring(i, j); Console.WriteLine(s1);
Console.WriteLine(s2);
Random rnd = new Random(Environment.TickCount);
int a = rnd.Next(0, str.Length - 1);
int b = a;
while (b == a) b = rnd.Next(0, str.Length - 1);
string result = str.Substring(a, 1) + str.Substring(b, 1);
Console.WriteLine(result);
char[] chastr = str.ToCharArray();
string code = "";
Random rd = new Random();
int i;
for (i = 0; i < num; i++)
{
code += str.Substring(rd.Next(0, str.Length), 1);
}
return code;
int a = rnd.Next(0, 数组.Length - 1);
用Linqstring str = "就是这些测试字符串";
Random rnd = new Random(Environment.TickCount);
string result = new string(str.ToCharArray().OrderBy(c=>rnd.Next()).Take(2).ToArray());
Random rnd = new Random();
int i1 = rnd.Next(0, strArr.Length);
int i2 = rnd.Next(0, strArr.Length);
for (; i2 == i1; )
{
i2 = rnd.Next(0, strArr.Length);
}
即strArr[i1]+strArr[i2]是你所要的二个随机数。
Random rnd = new Random();
int i1 = rnd.Next(0, strArr.Length - 1);
int i2 = rnd.Next(0, strArr.Length - 1);
for (; i2 == i1; )
{
i2 = rnd.Next(0, strArr.Length);
}
即strArr[i1]+strArr[i2]是你所要的二个随机数。刚才忘记数组未减1,现在改正
string[] str = { "aaa", "bbb", "ccc", "ddd" };Random rnd=new Random();int rc1=rnd.Next(0,strArr.Length - 1); //随机取数
int rc2=rnd.Next(0,strArr.Length - 1); //随机取数 string num1=str[rc1];
string num2=str[rc2];num1、num2 就是数组中随机取的任意两个字符串。