1

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.

Theodor Zoulias
  • 34,835
  • 7
  • 69
  • 104
prodigyak
  • 11
  • 1
  • According to this `int numberofParticipants` it is a signed 32 bit integer. Please post the exact error message. Also. `Random.Next(int minValue, int maxValue)` generates a number from `minValue` but smaller than `maxValue` (maxValue is exclusive). So to get a number from 1..20, you need `rand.Next(1, 21);` https://learn.microsoft.com/en-us/dotnet/api/system.random.next?view=net-7.0#system-random-next(system-int32-system-int32) – Eric J. Oct 24 '22 at 21:42
  • 1
    I would guess that what you are actually trying to do - is to output numbers from 1 to `numberofParticipants` in a shuffled order. To do that - create a list and fill it sequentially (e.g. `var list = Enumerable.Range(1, numberofParticipants).ToList();`) and then use one of these answers: https://stackoverflow.com/questions/273313/randomize-a-listt – bashis Oct 24 '22 at 21:45

2 Answers2

1
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();
mkmkmk
  • 167
  • 4
0

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!

prodigyak
  • 11
  • 1