-1

I'm currently working on a simulator, which should imitate a certain business logic. I try to create an object of a class Suggestion by picking two random strings out of two string arrays. The method should return an array of Suggestion objects. The array length is also variable. For now the returned array should be printed out in the console.

This is my class Suggestion:

 public class Suggestion
    {
        public Suggestion(string aktenName, string relevanz)
        {
            MAktenName = aktenName;
            MRelevanz = relevanz;
        }

        public string MAktenName { get; private set; }

        public string MRelevanz { get; private set; }
    }

My other class including the "logic" and the main method looks like this:

 private static void Main()
        {
            var test = GetSuggestions();

            for (var i = 0; i < test.Length; i++)
            {
                Console.WriteLine(test[i].MAktenName + "-" + test[i].MRelevanz + "%");
            }

            Console.ReadLine();
        }

        public static Suggestion[] GetSuggestions()
        {
            var random = new Random();
            var suggestionsCount = random.Next(1, 10);
            var suggestions = new Suggestion[suggestionsCount];

            string[] names =
            {
                "721/12", "345/93", "478/54", "876/39", "546/27", "481/56", "824/39", "897/51",
                "574/48"
            };
            string[] relevance =
            {
                "10", "20", "30", "40", "50", "60", "70", "80", "90", "100"
            };

            for (var i = 0; i < suggestionsCount; i++)
            {
              
                var sugName = names[new Random().Next(0, names.Length)];
                var sugRelevance = relevance[new Random().Next(0, relevance.Length)];

                suggestions[i] = new Suggestion(sugName, sugRelevance);
            }
            return suggestions;
        }

My problem is that it prints out a random combination of the string arrays sugName and sugRelevance and it prints a variable amount of these, but all the objects of the class Suggestions are the same. Like my result is something like:

345/93-20%

345/93-20%

345/93-20%

345/93-20%

I reckon it is something with the new Random().Next(0, names.Length) but I am not sure.

Community
  • 1
  • 1
Felix
  • 9
  • 1
  • 8

1 Answers1

0

Instead of

var sugName = names[new Random().Next(0, names.Length)];
var sugRelevance = relevance[new Random().Next(0, relevance.Length)];

Write:

var sugName = names[random.Next(0, names.Length)];
var sugRelevance = relevance[random.Next(0, relevance.Length)];