dimanche 29 novembre 2020

Generating a 6 digit unique random number

I want to create a 6 digit unique random number and check with existing numbers in the file and if its same, i need to generate another number until its unique.

While debugging I was able to find out that this is almost successful and if the number is same, it generates the new unique number but after the competition of the else if statement and when it returns back to the if statement to check if the new created number is equal to the first line read in the file, the program crashes and goes to the try catch statement.

Please note in "Customer.Txt" the unique number will be in the following order. First unique number will be in row 1, second one will be 9 and third will be 17 and hence forth.

Can anyone help me in figuring out what is the issue here?

Random MemeberShipId = new Random()
StreamReader InputFile
InputFile=File.OpenText("Customer Info.txt");
while(!InputFile.EndOfStream)
{
  X = int.Parse(MemeberShipId.Next(0, 999999).ToString("000000"));
  Y = int.Parse(InputFile.ReadLine());
  if(X!=Y)
{
for(int J=1;J<8;J++)
{
Temp=InputFile.ReadLine();
}
}
else if(Y==X)
{
MessageBox.Show("Same Unique Number Found, Creating another Unique number");
X=int.Parse(MemebershipId.Next(0,999999).ToString("000000);
}
}



Aucun commentaire:

Enregistrer un commentaire