Restart the while loop method

Asked

Viewed 40 times

0

I have a script to generate a certain amount of random Cpfs in a text file.

When executing the method that generates the random CPF within the while, the method is executed only once and always generates the same number in the text file.

using System;
using System.IO;
using geraCPF;

namespace geraTXT
{
    public class TXT
    {
        public CPF geraRand;

        [STAThread]
        static void Main(string[] args)
        {
             CPF geraRand = new CPF();

            //Informa o caminho e o nome do arquivo apra o construtor StreamWriter
            StreamWriter sw = new StreamWriter(@"C:\Users\carol\Desktop\CPFs.txt");

            //Escreve linhas no arquivo .txt
            for (int i = 0;  i < 10; i++)
            {
                sw.WriteLine(geraRand.cpfRandomico());
            } 

            //Fecha o arquivo
            sw.Close();          
        }
    }
}

Script result:

48307261058
48307261058
48307261058
48307261058
48307261058
48307261058
48307261058
48307261058
48307261058
48307261058

What needs to be done for the method to be executed at each increment of the i++ in the while, thus generating a different number each time?

I want to know how to run the start method again, with each increment, to generate a different number.

  • How to run the start method again at each increment to generate a different number?

  • What is the implementation of the class CPF ? the Random is instantiated in the constructor or cpfRandomico of that class ? If it is in the cpfRandomico is exactly the problem that @Maniero linked to in the duplicate question.

  • @Isac, Random was being declared var within the class cpfRandomico, hence the error. I instated out of class and worked.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.