随机字符在C#中显示为相同的字符?

时间:2014-03-07 18:37:04

标签: c# random char

该程序假设生成随机牌照号码。前3个字符是字母,后3个字符是数字。由于某种原因,后两个字母始终显示为相同的字母,并且所有三个数字保持相同的数字。

例如,当我希望所有字符都是随机的时,它会显示为WKK-555。

我做错了什么?

        // This is how the license plates are randomly created.
        StringBuilder sb = new StringBuilder();

        // first randomly chosen capital alphabetic character.                                           
        Random rng1 = new Random();
        char C1;
        int firstCharIndex = -1;
        string specialStr1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        firstCharIndex = rng1.Next(0, specialStr1.Length);
        C1 = specialStr1[firstCharIndex];
        sb.Append(C1);

        // second randomly chosen capital alphabetic character.
        Random rng2 = new Random();
        char C2;
        int secondCharIndex = -1;
        string specialStr2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        secondCharIndex = rng2.Next(0, specialStr2.Length);
        C2 = specialStr2[secondCharIndex];
        sb.Append(C2);

        // third randomly chosen capital alphabetic character. 
        Random rng3 = new Random();
        char C3;
        int thirdCharIndex = -1;
        string specialStr3 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        thirdCharIndex = rng2.Next(0, specialStr3.Length);
        C3 = specialStr3[thirdCharIndex];
        sb.Append(C3);

        // hyphen
        char C4;
        int fourthCharIndex = 0;
        string specialStr4 = "-";
        C4 = specialStr4[fourthCharIndex];
        sb.Append(C4);

        // first randomly selected digit.
        Random rng5 = new Random();
        char C5;
        int fifthCharIndex = -1;
        string specialStr5 = "0123456789";
        fifthCharIndex = rng5.Next(0, specialStr5.Length);
        C5 = specialStr5[fifthCharIndex];
        sb.Append(C5);

        // second randomly selected digit.
        Random rng6 = new Random();
        char C6;
        int sixthCharIndex = -1;
        string specialStr6 = "0123456789";
        sixthCharIndex = rng6.Next(0, specialStr6.Length);
        C6 = specialStr6[sixthCharIndex];
        sb.Append(C6);

        // third randomly selected digit.
        Random rng7 = new Random();
        char C7;
        int seventhCharIndex = -1;
        string specialStr7 = "0123456789";
        seventhCharIndex = rng7.Next(0, specialStr7.Length);
        C7 = specialStr7[seventhCharIndex];
        sb.Append(C7);

        // our license plate!!!
        LicensePlateTextBox.Text = sb.ToString();

4 个答案:

答案 0 :(得分:2)

您只需要创建和使用Random的一个实例。当您快速连续创建它们时,它们将具有相同的种子并返回相同的值。

答案 1 :(得分:1)

其他答案告诉你为什么没有得到正确的结果。这将生成您正在寻找的格式的牌照,并使用较少的重复代码(尽管可能还有更好的解决方案)。

Random r = new Random();
string plateNumber = "";
for (int i = 0; i < 3; i++) plateNumber += (char)r.Next(65, 90);
plateNumber += "-";
for (int i = 0; i < 3; i++) plateNumber += r.Next(0, 9).ToString();

答案 2 :(得分:0)

您每次都要实例化一个新的Random。默认情况下,种子是当前的tickcount。

由于它运行得非常快,它们都将具有相同的种子,因此第一个值将始终相同。要防止这种情况,请创建一个Random并多次调用下一个方法

答案 3 :(得分:0)

每个人都会将您的RNG实例化一次。在这里,您可以使用线程安全的车牌号码生成器:

class RandomLicensePlateNumberGenerator
{
  static readonly Random Randomizer = new Random() ;

  public string Generate( string pattern )
  {
    if ( string.IsNullOrWhiteSpace(pattern)) throw new ArgumentOutOfRangeException("pattern") ;
      return new string( pattern.Select( RandomCharacter ).ToArray() ) ;
  }

  public IEnumerable<string> Stream( string pattern )
  {
    while ( true )
    {
      yield return Generate( pattern ) ;
    }
  }

  public char RandomCharacter( char c )
  {
    char value ;
    lock ( Randomizer )
    {
      switch ( c )
      {
      case 'A' : case 'a' : value = (char) Randomizer.Next( 'A' , 'Z' + 1 ) ; break ;
      case '#' : case '9' : value = (char) Randomizer.Next( '0' , '9' + 1 ) ; break ;
      default  :            value = c                                       ; break ;
      }
    }
    return value ;
  }

}

class Program
{
  static void Main()
  {
    RandomLicensePlateNumberGenerator licensePlates = new RandomLicensePlateNumberGenerator();
    int i = 0 ;
    foreach ( string s in licensePlates.Stream( "AAA-999" ).Take(1000) )
    {
      Console.WriteLine( "{0,6:#,##0}: {1}",++i,s) ;
    }
    return ;
  }
}