-1

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;}
Alessio Cantarella's user avatar
Alessio Cantarella
5,2413 gold badges31 silver badges37 bronze badges
askedJan 20, 2018 at 11:03
xbk the second's user avatar
2
  • 3
    I don't see how you could possibly have missed theRandom class?CommentedJan 20, 2018 at 11:05
  • Please provide a clear problem statement related to the code you are presenting. The code contains a call toRandom.Next(), thus it's unclear what you are asking.CommentedJan 20, 2018 at 11:10

1 Answer1

1

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);
Stephen Kennedy's user avatar
Stephen Kennedy
21.8k24 gold badges99 silver badges114 bronze badges
answeredJan 20, 2018 at 11:10
Alessio Cantarella's user avatar
Sign up to request clarification or add additional context in comments.

1 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.