-3

This program is suppose to generate a random license plate number. The first 3 characters are letters and the second 3 characters are numbers. For some reason the second two letters always appear as the same letter and all three numbers remain as the same number.

For example it will appear like this WKK-555 when I want all characters to be random.

What am I doing wrong?

        // 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();
Learnin2Code
  • 133
  • 1
  • 7
  • 14
  • 2
    Instead of making a new instance of Random, just call rng1.Next(); – Ian P Mar 07 '14 at 18:38
  • 3
    And please don't duplicate that much code... – Brandon Mar 07 '14 at 18:38
  • possible duplicate of [Random number generator only generating one random number](http://stackoverflow.com/questions/767999/random-number-generator-only-generating-one-random-number) – L.B Mar 07 '14 at 18:39

4 Answers4

2

You need to create and use only one instance of Random. When you create them in quick succession, they will have the same seed and return the same values.

Jason P
  • 26,984
  • 3
  • 31
  • 45
1

The other answers tell you why you aren't getting the proper result. This will generate you a license plate in the format you're looking for and uses less duplicated code (although there's probably a solution that's better still).

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();
Brandon
  • 4,491
  • 6
  • 38
  • 59
0

You're instantiating a new Randomevery time. The seed by default is the current tickcount.

Since this runs very fast, they will all have the same seed and thus the first value will always be the same. To prevent this, create a single Random and call the next method several times

Kenneth
  • 28,294
  • 6
  • 61
  • 84
0

Instantiate your RNG just once as everybody sez. Here you go, a thread-safe license plate number generator:

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 ;
  }
}
Nicholas Carey
  • 71,308
  • 16
  • 93
  • 135