Creating Random objects WITHOUT duplicate objects

.Hello, good people. My task is to randomly determine the number of so-called survivors and list them. The problem is that the names of the survivors themselves are repeated, that is, my random calls one number from the interval several times

Random rnd = new Random(); //try not to repeat(one goats shouldn't be eatten for 2 times for example)
            int n = rnd.Next(0, 8); //how many survive by Random 
            Console.WriteLine("Выжило {0}, а именно: ", n);
            for (int i = 0; i < n; i++) //cyrcle with random number of iteration(how many alive?)
            {
                Random rand = new Random();
                int k = rand.Next(1, 7);
            switch (k)
            {
                case 1:

                    Console.WriteLine(Smarty.Name);
                    Console.WriteLine(" Цвет: {0}", Smarty.Color);
                    break;
                case 2:

                    Console.WriteLine(Bunt.Name);
                    Console.WriteLine(" Цвет: {0}", Bunt.Color);
                    break;
                case 3:
                    Console.WriteLine(Stomp.Name);
                    Console.WriteLine(" Цвет: {0}", Stomp.Color);
                    break;
                case 4:

                    Console.WriteLine(Talker.Name);
                    Console.WriteLine(" Цвет: {0}", Talker.Color);
                    break;
                case 5:
                    Console.WriteLine(Scum.Name);
                    Console.WriteLine(" Цвет: {0}", Scum.Color);
                    break;
                case 6:
                    Console.WriteLine(Teaser.Name);
                    Console.WriteLine(" Цвет: {0}", Teaser.Color);
                    break;
                case 7:
                    Console.WriteLine(Lil.Name);
                    Console.WriteLine(" Цвет: {0}", Lil.Color);
                    break;
                default:
                    Console.WriteLine("Таких у нас нету");
                    break;
            }
        }`

Accordingly, I want one case to work only once. There was an idea to create a List with the subsequent deletion of the object selected by random, but it did not work. I will be glad to hear the options your decisions. Thanks.

Author: Max Squirrel, 2019-08-10

3 answers

The idea with list is pretty good. And switch/case doesn't have to be used.

        // Создание списка выживших
        List<string> alives = new List<string> { "Tom", "Sendi", "Rassel", "Mike", "Emma", "Olivia", "Mia", "Ethan" };

        Console.WriteLine("Welcome to dead game!\nNames players:\n");

        // Вывод списка на екран
        foreach (string player in alives)
        {
            Console.Write("{0}  ", player);
        }

        Console.WriteLine("\nStart game!");

        Random rand = new Random();
        int next;
        // Убийство наших вишывших
        do
        {
            next = rand.Next(0, alives.Count);

            Console.WriteLine("Dead is - {0}", alives[next]);

            alives.RemoveAt(next);
        } while (alives.Count != 1);

        Console.WriteLine("Winner is a {0}", alives[0]);

Result

Welcome to dead game!
Names players:

Tom  Sendi  Rassel  Mike  Emma  Olivia  Mia  Ethan
Start game!
Dead is - Olivia
Dead is - Mike
Dead is - Rassel
Dead is - Tom
Dead is - Sendi
Dead is - Emma
Dead is - Ethan
Winner is a Mia
 2
Author: JuniorOne, 2019-08-10 14:11:47

You can create an array of keys and shuffle them.

It's also good to put all the people in an array for ease of processing.

   int[] keys =  {1,2,3,4,5,6,7};
   personType[] person = {Smarty,Bunt,Stomp,Talker, Scum, Teaser, Lil};
   for (i=1; i<10; i++){
     int t = rand.Next(0, 6);
     int s = rand.Next(0, 6);
     swap(keys[t],keys[s]);
   }
   for (int i = 0; i < n; i++) 
      {
         Console.WriteLine(person[keys[i]].Name);
         Console.WriteLine(" Цвет: {0}", person[keys[i]].Color);
      }   
 2
Author: becouse, 2019-08-10 13:53:21

There is no such method out of the box. You need to write a class yourself that will give you random names from the list.

Let's say something like

public static class RndNameGenerator
{
    Random static rnd = new Random();
    public static list<string> Gen(list<string> namesBase, int resultCount)
   {
        var namesLst= namesBase.Clone();
        var lstRez = new list<string>(); 

        for(int i=0; i<= resultCount; i++)
        {
            int itemNum = rand.Next(0, namesBase.Count() );
            lstRez.Add(namesLst(itemNum));
            lst.RemoveAt(itemNum);
        }

        return lstRez;
   }
}

And by contacting

RndNameGenerator.Gen(someNameList, 3);

It will return 3 uncal names. (true, if there are no duplicates in the someNameList itself)

The code, of course, was not checked.

 1
Author: Andrew, 2019-08-10 13:57:04