求高手,帮我写一段代码,循环产生不同的八个字符的 key.例如:   nGa[rwkH
不知道.为什么,我循环,产生出来的 key 都是相同的.

解决方案 »

  1.   


    用下面范围内的字符生成八位字符的Key.
    A-Z
    a-z
    0-9
    []像这样的:  nGa[rwkH 
      

  2.   

    key是相同的八成是LZ的Random使用有误.
    把Random变量改成静态变量试试.
      

  3.   

    如果是要得到每个字符
    用foreach循环遍历就好了 
      

  4.   

    using System;
    using System.Collections.Generic;
    using System.Text;namespace radam
    {
        class Program
        {
            static int GetRandomSeed()
            {
                byte[] bytes = new byte[4];
                System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider();
                rng.GetBytes(bytes);
                return BitConverter.ToInt32(bytes, 0);
            }        static void Main(string[] args)
            {
                string[] a ={"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","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","0","1","2","3","4","5"
                    ,"6","7","8","9","[","]"};
                string s = string.Empty;
                for (int i = 0; i <8;i++ )
                {
                    Random r = new Random(GetRandomSeed());
                    int j=r.Next(0,63);
                     s = s + a[j];
                     
                   
                }
                Console.WriteLine(s);
                            }
            }
        
    }
      

  5.   


    string s = string.Empty;
                List<string> key = new List<string>();
                for (int k=0;k<1000;k++)
                {
                    for (int i = 0; i < 8; i++)
                    {
                        Random r = new Random(GetRandomSeed());
                        int j = r.Next(0, 63);
                        s = s + a[j];
                    }
                    if (!key.Contains(s))
                        key.Add(s);
                    else
                        s = string.Empty;
                }
      

  6.   

    static int GetRandomSeed()
            {
                byte[] bytes = new byte[4];
                System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider();
                rng.GetBytes(bytes);
                return BitConverter.ToInt32(bytes, 0);
            } string s = string.Empty;
                List<string> key = new List<string>();
              whilie(key.Count<8)
                {
                    for (int i = 0; i < 8; i++)
                    {
                        Random r = new Random(GetRandomSeed());
                        int j = r.Next(0, 63);
                        s = s + a[j];
                    }
                    if (!key.Contains(s))
                        key.Add(s);
                    else
                        s = string.Empty;
                }