mercredi 14 juin 2017

generate random list in c# working in debug mode only

For testing some algorithm complexity I need to generate two list with different data, that's why I write some following code. the problem that when I run the code I get two list each one contains the same word (and the same char) instead the random concept that I implemented but when I debug my code step by step and I dig into each method to find what is coming wrong I get the expect result (means random words) see the image bellow. any could explain what is wrong??

my code

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

namespace testReg
{
class Program
{
    static void Main(string[] args)
    {

        var list1 = ListGenerator.GetFirstList();
        var list2 = ListGenerator.GetSecondList(list1);
        Console.ReadLine();
    }

}

public class FirstClass
{
    public int Id { get; set; }
    public string LName { get; set; }
    public string FName { get; set; }
    public int Age { get; set; }

}

public class SecondClass
{
    public int Xid { get; set; }
    public string XlName { get; set; }
    public string XFName { get; set; }
    public int XAge { get; set; }
}

public static class ListGenerator
{

    public  static List<FirstClass> GetFirstList()
    {
        List<FirstClass> list = new List<FirstClass>();
        for (int i = 0; i < 1000; i++)
        {
            var item = new FirstClass();
            item.Id = i;
            item.LName = RandomWord(8);
            item.FName = RandomWord(10);
            item.Age = i * 3 - (i - (i-3)*2);
            list.Add(item);
        }
        return list;
    }
    public static List<SecondClass> GetSecondList(List<FirstClass> firstList)
    {
        List<SecondClass> list = new List<SecondClass>();
        for (int i = 0; i < 1000; i++)
        {
            SecondClass secondList = new SecondClass();
            if (i % 7 == 0)
            {
                secondList.Xid = firstList.ElementAt(i).Id;
                secondList.XlName = firstList.ElementAt(i).LName;
                secondList.XFName = firstList.ElementAt(i).FName;
                secondList.XAge = i * 3 - (i - (i - 3) * 2);
                list.Add(secondList);
            }else if (i % 9 == 0)
            {
                secondList.Xid =i;
                secondList.XlName = firstList.ElementAt(i).LName;
                secondList.XFName = RandomWord(13);
                secondList.XAge = i * 3 - (i - (i - 3) * 2);
                list.Add(secondList);
            }
            else 
            {
                secondList.Xid = i;
                secondList.XlName = RandomWord(8);
                secondList.XFName = RandomWord(13);
                secondList.XAge = i;
                list.Add(secondList);
            }
        }
        return list;
    }
    public  static string RandomWord(int requestedLength)
    {
        string[] consonants = { "b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "n", "p", "q", "r", "s", "t", "v", "w", "x", "y", "z" };
        string[] vowels = { "a", "e", "i", "o", "u" };

        string word = "";

        if (requestedLength == 1)
        {
            word = GetRandomLetter(vowels);
        }
        else
        {
            for (int i = 0; i < requestedLength; i += 2)
            {
                word += GetRandomLetter(consonants) + GetRandomLetter(vowels);
            }

            word = word.Replace("q", "qu").Substring(0, requestedLength); // We may generate a string longer than requested length, but it doesn't matter if cut off the excess.
        }

        return word;
    }
    private static string GetRandomLetter(string[] letters)
    {
        Random r = new Random();
        int rInt = r.Next(0, letters.Length - 1);

        return letters[rInt];
    }
}


}

enter image description here




Aucun commentaire:

Enregistrer un commentaire