在一个字符串数组中取任意两个字符串,怎么写?谢谢

解决方案 »

  1.   

    Random
    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);
      }
      

  2.   

    Random rd = new Random(Guid.NewGuid().GetHashCode())
      

  3.   


    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);
      

  4.   

    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);
      

  5.   

    相当于登录时候的验证码,对不?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;
      

  6.   

    Random rnd=new Random(DateTime.Now.Millisecond); 
    int a = rnd.Next(0, 数组.Length - 1);
      

  7.   

    如果.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());
      

  8.   

    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]是你所要的二个随机数。
      

  9.   

    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,现在改正
      

  10.   


    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 就是数组中随机取的任意两个字符串。