简体   繁体   中英

Select a random data from a text file and put it in a specific locations in an Excel spreadsheet

I have a question: how can I select one single random piece of data from a file and put it inside of a .csv file? I know you need to set up the path to locate the file in your computer but I would like to know when to use the random() method or another method to use random since in C#, random is mostly for integers and doubles.

For example: I have this list of names inside of a text file:

Kenneth
Samuel
Samantha
Catherine
Danielle
Jonathan
Ellen
Valentin
Christopher
Edward

Now, I would like to put one of these names inside a .CSV file, in a specific location. Here is what I've tried so far:

namespace ConsoleApp
{
    class Program
    {

        public static List<RecordStructure> CSVRecords = new List<RecordStructure>();

        static void Main(string[] args)
        {
            Console.WriteLine("Data Mask Process");
            Console.WriteLine("**************************");

            CSVRecords.AddRange(ReadCSVFile());

            // some things I wrote for a work :)
            string contentF_names = File.ReadAllText(@"C:\\path");
            string contentM_names = File.ReadAllText(@"C:\\path");
            string contentNames = File.ReadAllText(@"C:\\path");
            string contentPlaces = File.ReadAllText(@"C\\path");

            // For the random part
            Random r = new Random();

            var line = contentF_names[r.Next(contentF_names.Length)];

            // Printing and ending

            Console.WriteLine(CSVRecords[1].firstname);
            Console.WriteLine(CSVRecords[1].lastname);
            Console.WriteLine(CSVRecords[1].city);

            Console.WriteLine("**************************");
            Console.WriteLine("End of Process.");
            Console.ReadKey();
        }

        public static List<RecordStructure> ReadCSVFile()
        {
            List<RecordStructure> RecordList = new List<RecordStructure>();

            StreamReader reader = new StreamReader(File.OpenRead(@"path"));

            while (!reader.EndOfStream)
            {
                string line = reader.ReadLine();

                if (!String.IsNullOrWhiteSpace(line))
                {
                    string[] values = line.Split(',');

                    RecordList.Add(new RecordStructure
                                       {
                                           firstname = values[0],
                                           lastname = values[1],
                                           company_name = values[2],
                                           city = values[3]
                                       });
            }
        }

        return RecordList;
    }
}

class RecordStructure
{
    public string firstname { get; set; }
    public string lastname { get; set; }
    public string company_name { get; set; }
    public string address { get; set; }
    public string city { get; set; }
    public string country { get; set; }
    public string state { get; set; }
    public string zip { get; set; }
    public string phone1 { get; set; }
    public string phone2 { get; set; }
    public string email { get; set; }
    public string web { get; set; }
}

Since you're working on CVS files, you can convert the result to Array or List , then use its index with the Random to get a random index.

Example:

var contentF_names = "Kenneth,Samuel,Samantha,Catherine,Danielle,Jonathan,Ellen,Valentin,Christopher,Edward";

// ToList
var contentF_namesArray = contentF_names.Split(',').ToList();

//For the random part

Random r = new Random();

// Get Random index between 0 and the total number of elements in the list
var randomIndex = r.Next(0, contentF_namesArray.Count);

// Use the random index to get an element from the list.
var randomFirstName = contentF_namesArray[randomIndex];

the randomFirstName will hold the random name, and each time you run. You could use a loop to get x number of random names.

Example:

/*
    Get Five Random Names 
*/

for (int x =0; x <= 5; x++)
{
    // Get Random index between 0 and the total number of elements in the list
    var randomIndex = r.Next(0, contentF_namesArray.Count);

    // Use the random index to get an element from the list.
    var randomFirstName = contentF_namesArray[randomIndex];

    Console.WriteLine(randomFirstName);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM