Unlock For Us

Generate random value between minimum and upperbound in VB.NET

 To generate a random value between a lowerBound and an upperBound, use the standard scaling formula: random() * (upperBound - lowerBound) + lowerBound for decimal numbers, or floor(random() * (upperBound - lowerBound + 1)) + lowerBound for inclusive integers. Most programming languages and spreadsheet tools also provide built-in functions to do this directly.


VB.NET


' Initialize the random-number generator. Randomize() ' Generate random value between 1 and 6. Dim value As Integer = CInt(Int((6 * Rnd()) + 1))
messagebox.show(value.tostring())



The Rnd function returns a value less than 1, but greater than or equal to zero. Before calling Rnd, use the Randomize statement without an argument to initialize the random-number generator with a seed based on the system timer.


References:

Google

https://learn.microsoft.com/en-us/dotnet/api/microsoft.visualbasic.vbmath.rnd?view=net-10.0

0 Comments:

 

© Unlock For Us @2026-2027| Blogger| License Agreement| Author| Contact Us