hatsjie hatsjie -4 years ago 57
C# Question

C# program generates same output in file different when adding breakpoints

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--)
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);

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download