Reputation: 11
I want to preface this with the following: I'm super new to programming. So my brain is still trying to figure all of the pieces of the puzzle out. Currently, I'm trying to sharpen my skills by writing some basic console applications. I'm currently writing an application to actually help a friend who hosts tournaments. He simply would like something that will accept input and generate random numbers equal to the inputted value. I have this part functional and working with the code below.
using System;
using System.Collections.Generic;
Console.WriteLine("Please Enter the number of participants!");
var rand = new Random();
Console.WriteLine("Please Enter the number of participants!");
int numberofParticipants = Convert.ToInt32(Console.ReadLine());
List<int> listNumbers = new List<int>();
int number;
for (int i = 0; i < numberofParticipants; i++)
{
do
{
number = rand.Next(1, 20);
} while (listNumbers.Contains(number));
listNumbers.Add(number);
}
listNumbers.ForEach(Console.WriteLine);
Console.ReadLine();
The thing is I would like to take this a step farther by implementing a mechanism that caps the max value to the same value of the number of participants.
So in other words this section of code is causing me issues:
number = rand.Next(1, 20);
I have attempted to change 20 for the numberofParticipants variable but I get an error in regards to it not being a signed 32 bit int. So in the end the user should be able to input a number, say 10. This will then output 10 numbers (1-10) with the max value of 10. So in essence a random order of numbers from 1-10.
Is there any easy solution for this that I'm not aware of?
Thank you in advance for any input and direction for this total newb.
Upvotes: 1
Views: 610
Reputation: 11
Thank you for the feedback everyone. Utilizing the list with the max number set to the participants and then utilizing the Guid to shuffle is what I went with. This works perfectly for the context of this application.
Thanks!
Upvotes: 0
Reputation: 197
using System;
using System.Collections.Generic;
Console.WriteLine("Please Enter the number of participants!");
var rand = new Random();
Console.WriteLine("Please Enter the number of participants!");
int numberofParticipants = Convert.ToInt32(Console.ReadLine());
List<int> listNumbers = new List<int>();
int number;
for (int i = 0; i < numberofParticipants; i++)
{
do
{
//you need to input parameter as
//numberofParticipants
number = rand.Next(1, numberofParticipants);
} while (listNumbers.Contains(number));
listNumbers.Add(number);
}
listNumbers.ForEach(Console.WriteLine);
Console.ReadLine();
To write in a simpler code, you can do it as follows.
using System;
using System.Collections.Generic;
Console.WriteLine( "Please Enter the number of participants!" );
int numberofParticipants = Convert.ToInt32( Console.ReadLine() );
Random rand = new Random();
List<int> listNumbers = new List<int>( Enumerable.Range( 1, numberofParticipants ).OrderBy( n => rand.Next() ) );
listNumbers.ForEach( Console.WriteLine );
Console.ReadLine();
Upvotes: 1