All answers still have problems or errors (plural, not just one). I will explain. But first, I want to praise Dan Tao to use the static variable to remember the Generator variable, so repeating it repeatedly will not repeat the same # again and again, plus he gave a very good explanation. But his code suffered the same flaw as most others, as I now explain.
MS made its Next () method pretty odd. the Min parameter is an inclusive minimum, as you would expect, but the Max parameter is an exclusive maximum that would not be expected. in other words, if you pass min = 1 and max = 5, then your random numbers will be any of 1, 2, 3 or 4, but it will never include 5. This is the first of two potential errors in the whole code, uses the Microsoft method Random.Next ().
For a simple answer (but still with other possible but rare problems) you will need to use:
Private Function GenRandomInt(min As Int32, max As Int32) As Int32 Static staticRandomGenerator As New System.Random Return staticRandomGenerator.Next(min, max + 1) End Function
(I like to use Int32 , not Integer , because it makes it clearer how big the int is, plus it is shorter, but suits you yourself.)
I see two potential problems with this method, but it will be suitable (and correct) for most applications. Therefore, if you want a simple solution, I believe that this is correct.
The only 2 problems that I see with this function are: 1: when Max = Int32.MaxValue, so adding 1 creates a numerical overflow. altho, that would be rare, it is still possible. 2: when min> max + 1, when min = 10 and max = 5, the Next function throws an error. it may be what you want. but it may not be. or consider when min = 5 and max = 4. By adding 1, 5 the Next method is passed, but it does not throw an error when it really is an error, but the Microsoft.NET code I tested returns 5. so it really is not "exclusive "maximum when max = min. but when max <min for the Random.Next () function, then it throws an ArgumentOutOfRangeException. therefore, the Microsoft implementation is actually inconsistent and erroneous in this regard.
you can just change the numbers at min> max so that there are no errors, but it completely depends on what you want. if you need an error with invalid values, then it is probably better to throw an error when the exclusive Microsoft maximum (max + 1) in our code is equal to the minimum where MS cannot make an error in this case.
processing a workflow when max = Int32.MaxValue is a bit inconvenient, but I expect to post a full function that handles both of these situations. and if you need a different behavior than the way I encoded it, come on. but keep these 2 issues in mind.
Happy coding!
Edit: So I need a random number generator, and I decided to encode it βcorrectlyβ. Therefore, if someone wants to get full functionality, then here that really works. (But he does not win the simplest prize with only two lines of code, but he is also not very complicated.)
''' <summary> ''' Generates a random Integer with any (inclusive) minimum or (inclusive) maximum values, with full range of Int32 values. ''' </summary> ''' <param name="inMin">Inclusive Minimum value. Lowest possible return value.</param> ''' <param name="inMax">Inclusive Maximum value. Highest possible return value.</param> ''' <returns></returns> ''' <remarks></remarks> Private Function GenRandomInt(inMin As Int32, inMax As Int32) As Int32 Static staticRandomGenerator As New System.Random If inMin > inMax Then Dim t = inMin : inMin = inMax : inMax = t If inMax < Int32.MaxValue Then Return staticRandomGenerator.Next(inMin, inMax + 1) ' now max = Int32.MaxValue, so we need to work around Microsoft quirk of an exclusive max parameter. If inMin > Int32.MinValue Then Return staticRandomGenerator.Next(inMin - 1, inMax) + 1 ' okay, this was the easy one. ' now min and max give full range of integer, but Random.Next() does not give us an option for the full range of integer. ' so we need to use Random.NextBytes() to give us 4 random bytes, then convert that to our random int. Dim bytes(3) As Byte ' 4 bytes, 0 to 3 staticRandomGenerator.NextBytes(bytes) ' 4 random bytes Return BitConverter.ToInt32(bytes, 0) ' return bytes converted to a random Int32 End Function