0

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

monther zlatan
  • 156
  • 1
  • 14
  • 2
    You should use a global Random variable, and reuse it for each call. Using a new Random variable each time will most likely seed it with the exact same seed, causing this issue. – Zac Faragher Jun 14 '17 at 23:17
  • 2
    this is a very common question - please see the linked post or just google ".net random not working" or "c# random not working". Basically, the problem is that `new Random()` is **time based** (at least, historically). You need to *retain* a `Random` instance if you're going to call lots of times in a loop. When you use breakpoints and step through: **you change how that works** - because you've added delays – Marc Gravell Jun 14 '17 at 23:18
  • 1
    @ZacFaragher - There are no global variables in C#. What's closest is a `[ThreadLocal] static Random` variable defined once in a class. – Enigmativity Jun 15 '17 at 00:10
  • @Enigmativity I think that's what Zac Faragher means. define a variable once in a class and send it as parameter to methods that render Random words – monther zlatan Jun 15 '17 at 00:23
  • @slatniawadii - Yes, I understood that. Calling it a "global variable" might be confusing to some and I was just clarifying, – Enigmativity Jun 15 '17 at 01:40

0 Answers0