This question already has answers here:
How do I generate a random integer in C#? (27 answers)
Closed7 years ago.
I've tried everything from YouTube videos to this forum.I used to find always a solution here but now I'm stuck.I need to generate a random number between 39 and 52.
Here's the somewhat source:
case Form1.number.WITHRANDOM:{ int i = 0; while (i < ammount) { i++; int j = 0; string text2 = ""; while (j < 2) { string value = Conversions.ToString(this.random.Next(0, text.Length)); text2 += Conversions.ToString(text[Conversions.ToInteger(value)]); j++; } this.numberList.Add("173" + (The random number) + text2); } break;}- 3I don't see how you could possibly have missed the
Randomclass?Visual Vincent– Visual Vincent2018-01-20 11:05:08 +00:00CommentedJan 20, 2018 at 11:05 - Please provide a clear problem statement related to the code you are presenting. The code contains a call to
Random.Next(), thus it's unclear what you are asking.Ondrej Tucny– Ondrej Tucny2018-01-20 11:10:35 +00:00CommentedJan 20, 2018 at 11:10
1 Answer1
You should use theRandom class. ItsNext method returns a random integer within a specified range (betweenminValue andmaxValue):
public virtual int Next(int minValue, int maxValue)So, in your case, this is the code:
Random random = new Random();int number = random.Next(39, 52); Sign up to request clarification or add additional context in comments.
1 Comment
Protiguous
Microsoft Docs: The Next(Int32, Int32) method allows you to specify the range of the returned random number. However, the maxValue parameter, which specifies the upper range returned number, is an exclusive, not an inclusive, value. This means that the method call Next(0, 100) returns a value between 0 and 99, and not between 0 and 100.
Explore related questions
See similar questions with these tags.

