1

I want to generate 160 different random users and passwords, now the code seems to work fine when I add breakpoints on user and password variables (in the for loop). But when I run my code without breakpoints, the Passwords.csv has the same output for all 160 lines. Any help is appreciated

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace WriteCSVPasswords
{
    class Program
    {
        static void Main(string[] args)
        {
            string path = @"c:\temp\Passwords.csv";
            string user = null;
            string password = null;
            string[] file = new string[160];

            for (int i = 0; i < 160; i++)
            {
                user = CreatePassword(4);
                password = CreatePassword(5);
                file[i] = user + ";" + password + ";" + Base64Encode(user + ":" + password);
                user = null;
                password = null;
            }

            if (!File.Exists(path))
            {
                // Create a file to write to.
                File.WriteAllLines(path, file, Encoding.UTF8);
            }



        }

        public static string CreatePassword(int length)
        {
            const string valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890.+-!";
            StringBuilder res = new StringBuilder();
            Random rnd = new Random();
            while (0 < length--)
            {
                res.Append(valid[rnd.Next(valid.Length)]);
            }
            return res.ToString();
        }

        public static string Base64Decode(string base64EncodedData)
        {
            var base64EncodedBytes = System.Convert.FromBase64String(base64EncodedData);
            return System.Text.Encoding.UTF8.GetString(base64EncodedBytes);
        }
        public static string Base64Encode(string plainText)
        {
            var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
            return System.Convert.ToBase64String(plainTextBytes);
        }
    }
}
hatsjie
  • 181
  • 10

1 Answers1

5

I doubt this has to do with breakpoints. (Code Gray makes an excellent point about the timing difference when using breakpoints, so it is related to that.)

For sure you have an issue with Random rnd = new Random();, since you recreate the random generator, it will start all over again and again.

I would suggest to declare rnd at the class level.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • 3
    Probably *is* related to breakpoints, at least in the sense that if he pauses execution for a while, the time will be different, and thus a different random number will be generated. :-) – Cody Gray - on strike Jun 14 '17 at 12:18
  • Okay, good point @CodyGray – Patrick Hofman Jun 14 '17 at 12:20
  • Indeed, rnd at class level gets me the right answer. @CodyGray now I also fully understand why this peculiar behaviour happend, not so peculiar after all ;) Thx! – hatsjie Jun 14 '17 at 12:46