-1

i did write , but it gets just the nam:

var namenListe = new List<string>();
while(true)
{
    Console.WriteLine("Geben Sie die Namen ein ; "); 
    string name = Console.ReadLine();
    Console.WriteLine("Die namen : ");
    namenListe.Add(name);

    for (int i = 0; i < namenListe.Count; i++)
    {
        Console.WriteLine(namenListe);
    }
ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
Hadissnp
  • 31
  • 6
  • 4
    1. Suffle the list (see [Fisher-Yates Shuffle](https://stackoverflow.com/a/1262619/3181933)). 2. [Take the first N items from the list](https://stackoverflow.com/a/319976/3181933). – ProgrammingLlama Aug 30 '21 at 08:04
  • Also, why have you tagged `[n]`? The tag description for it is: _"n is a CLI for managing multiple Node.js versions."_ - I don't understand how this relates to your question about C#? I've removed it for you. – ProgrammingLlama Aug 30 '21 at 08:13
  • @Llama Because *"get **n** random names"* ? ¯\\_(ツ)_/¯ – Cid Aug 30 '21 at 10:19

2 Answers2

2
    private static readonly Random random = new();

    static void Main(string[] args)
    {
        while (true)
        {
            Console.WriteLine("Enter a set of comma separated name here:");
            var names = Console.ReadLine();
            
            if (string.IsNullOrWhiteSpace(names))
            {
                Console.WriteLine("Bad name input, try again \n");
                continue;
            }
            
            Console.WriteLine("Enter number of randomly selcted named:");
            var count = Console.ReadLine();

            if (!int.TryParse(count, out var nameCount))
            {
                Console.WriteLine("Bad name count input, try again \n");
                continue;
            }

            // Split the names.
            var nameCollection = names.Split(',', StringSplitOptions.TrimEntries).ToList();
            
            // Shuffle names.
            nameCollection = nameCollection.OrderBy(_ => random.Next()).ToList();
            
            Console.WriteLine($"Your randomly selected {nameCount} names are : {string.Join(", ", nameCollection.Take(nameCount))}\n\n");
        }
    }
FunksMaName
  • 2,101
  • 1
  • 15
  • 17
  • but i want sth like this : for example : input: sara, jana, jan, paul and then when i write 3 , i become 3 random names like this for example : output = jan,jana,sara – Hadissnp Aug 30 '21 at 09:56
-3

I think you will need to create a random number in a range from your minimum desired number of names to your maximum, then Use that to determine whether or not a name gets printed.. Like if your random number is five.. int x=0; if (x<ran) {print name(); x++;} If (x==ran) return; run this kind of code in your for loop instead of just printing all names.. But you'll have to generate your own random number.. the code is something like. int ran; ran =Random %max *100 +minimum; You'll have to check to be sure. And include Math.h or some other header to make sure random works.