Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Random integer in VB.NET

Tags:

random

vb.net

I need to generate a random integer between 1 and n (where n is a positive whole number) to use for a unit test. I don't need something overly complicated to ensure true randomness - just an old-fashioned random number.

How would I do that?

like image 888
brendan Avatar asked Aug 20 '08 19:08

brendan


People also ask

How do you generate a random integer in VB?

Random numbers can be generated by the Visual Basic Rnd() function, that returns a floating-point value between 0.0 and 1.0. Multiplying the random numbers will specify a wider range. For example, a multiplier of 20 will create a random number between zero and 20.

What is RND in VB net?

Rnd() Returns a random number of type Single. Rnd(Single) Returns a random number of type Single.

What is random integer in coding?

The rand function in C In C rand() returns a value between 0 and a large integer (called by the name "RAND_MAX" found in stdlib. h).


2 Answers

As has been pointed out many times, the suggestion to write code like this is problematic:

Public Function GetRandom(ByVal Min As Integer, ByVal Max As Integer) As Integer     Dim Generator As System.Random = New System.Random()     Return Generator.Next(Min, Max) End Function 

The reason is that the constructor for the Random class provides a default seed based on the system's clock. On most systems, this has limited granularity -- somewhere in the vicinity of 20 ms. So if you write the following code, you're going to get the same number a bunch of times in a row:

Dim randoms(1000) As Integer For i As Integer = 0 to randoms.Length - 1     randoms(i) = GetRandom(1, 100) Next 

The following code addresses this issue:

Public Function GetRandom(ByVal Min As Integer, ByVal Max As Integer) As Integer     ' by making Generator static, we preserve the same instance '     ' (i.e., do not create new instances with the same seed over and over) '     ' between calls '     Static Generator As System.Random = New System.Random()     Return Generator.Next(Min, Max) End Function 

I threw together a simple program using both methods to generate 25 random integers between 1 and 100. Here's the output:

Non-static: 70 Static: 70 Non-static: 70 Static: 46 Non-static: 70 Static: 58 Non-static: 70 Static: 19 Non-static: 70 Static: 79 Non-static: 70 Static: 24 Non-static: 70 Static: 14 Non-static: 70 Static: 46 Non-static: 70 Static: 82 Non-static: 70 Static: 31 Non-static: 70 Static: 25 Non-static: 70 Static: 8 Non-static: 70 Static: 76 Non-static: 70 Static: 74 Non-static: 70 Static: 84 Non-static: 70 Static: 39 Non-static: 70 Static: 30 Non-static: 70 Static: 55 Non-static: 70 Static: 49 Non-static: 70 Static: 21 Non-static: 70 Static: 99 Non-static: 70 Static: 15 Non-static: 70 Static: 83 Non-static: 70 Static: 26 Non-static: 70 Static: 16 Non-static: 70 Static: 75 
like image 83
Dan Tao Avatar answered Sep 19 '22 14:09

Dan Tao


To get a random integer value between 1 and N (inclusive) you can use the following.

CInt(Math.Ceiling(Rnd() * n)) + 1 
like image 44
Kibbee Avatar answered Sep 19 '22 14:09

Kibbee