0

I'm developing a program that generates up to 2 valid IMEI codes at a time for mobile phone testing. Although there is a method for each of the 2 codes, the program is always returning the same value for both (e.g. IMEI 1 = IMEI 2). Here's a sample of both methods (only the relevant parts).

IMEI 1:

public String IMEICode()
    {
        int[] code = new int[14];
        Random generate = new Random();
        int format = FormatCombo.SelectedIndex;
        StringBuilder IMEI = new StringBuilder();
        ... //irrelevant
        for (int i = 0; i < code.Length; i++)
        {
            code[i] = generate.Next(10);
        }
        ... //irrelevant
        return IMEI.ToString();
    }

IMEI 2:

public String IMEICode2()
    {
        int[] code2 = new int[14];
        Random generate2 = new Random();
        int format = FormatCombo.SelectedIndex;
        StringBuilder IMEI2 = new StringBuilder();
        ... //irrelevant
        for (int i = 0; i < code2.Length; i++)
        {
            code2[i] = generate2.Next(10);
        }
        ... //irrelevant
        return IMEI2.ToString();
    }

The program has 2 text boxes to display the generated codes (Field1 and Field2), each assigned to a method (IMEICode() and IMEICode2()).

PS.: I made sure that the first text box displays the IMEICode() method and the latter displays the IMEICode2() method.

92AlanC
  • 1,327
  • 2
  • 14
  • 33

1 Answers1

4

The default seed value is derived from the system clock and has finite resolution. As a result, different Random objects that are created in close succession by a call to the default constructor will have identical default seed values and, therefore, will produce identical sets of random numbers. This problem can be avoided by using a single Random object to generate all random numbers. You can also work around it by modifying the seed value returned by the system clock and then explicitly providing this new seed value to the Random(Int32) constructor. (from MSDN)

Use same Random object for both cases.

    public static Random generate = new Random();

    public String IMEICode()
        {
            int[] code = new int[14];
            int format = FormatCombo.SelectedIndex;
            StringBuilder IMEI = new StringBuilder();
            ... //irrelevant
            for (int i = 0; i < code.Length; i++)
            {
                code[i] = generate.Next(10);
            }
            ... //irrelevant
            return IMEI.ToString();
        }

    public String IMEICode2()
        {
            int[] code2 = new int[14];
            int format = FormatCombo.SelectedIndex;
            StringBuilder IMEI2 = new StringBuilder();
            ... //irrelevant
            for (int i = 0; i < code2.Length; i++)
            {
                code2[i] = generate2.Next(10);
            }
            ... //irrelevant
            return IMEI2.ToString();
        }
Farhad Jabiyev
  • 26,014
  • 8
  • 72
  • 98
  • 1
    `Random` class is not thread-safe, so you are treading on thin ice here by relying on the threading nature of the test runner. Better using `ThreadLocal` instead. – tia Jan 11 '15 at 09:35
  • More info in thread safety and random at http://stackoverflow.com/questions/3049467/is-c-sharp-random-number-generator-thread-safe – Richard Schneider Jan 11 '15 at 11:50