I will in this tutorial show you how you could get random numbers using VB.NET. Before we start I have to tell you that it's impossible to get a completely random number with a computer since they're all following different calculation. But the random numbers you can get is in most cases "random enough". The random number generator we'll use has this description:
So the numbers you'll get will be "random enough" even though they aren't actually random. Never mind, let's start.Represents a pseudo-random number generator, a device that produces a sequence of numbers that meet certain statistical requirements for randomness.
To create a random generator we just uses the data type Random, like so:
[highlight=VB.NET] Dim r As New Random[/highlight]
To then get a random positive integer number within the span of an integer value in VB.NET use:
[highlight=VB.NET]r.Next()[/highlight]
But you have to be careful if you want to generate more then one random number, if you do like this all numbers will be the same:
[highlight=VB.NET] For i As Integer = 0 To 99
Dim r As New Random
r.Next()
Next[/highlight]
To make them all different you have to do this:
[highlight=VB.NET] Dim r As New Random
For i As Integer = 0 To 99
r.Next()
Next[/highlight]
Even though we're doing this the 100 numbers will be "random" but if we call the function(or where we now have this code) again, the numbers won't be the same as before but they could look a bit like them(all depending on how often and when we calls the function(or whatever it is) again, but they could look very much the same). So therefor I recommend you to declare it as "unlocal" as you can to get the "most random" result, like this:
[highlight=VB.NET]Public Class frmMain
Dim r As New Random
Private Sub mySub()
For i As Integer = 0 To 99
r.Next()
Next
End Sub
End Class[/highlight]
An example on this could be an old code I made which took a lot of puzzle pieces and randomized there positions, It added one piece at a random interval too but when I randomized its position I declared a new Random generator each time which resulted in this (It doesn't look so random, right?):
You can also set a exclusive maximal value the Random Generator can generate, like this:
[highlight=VB.NET] r.Next(11)[/highlight]
Or both the inclusive minimal value and the exclusive maximal:
[highlight=VB.NET] r.Next(5, 11)[/highlight]
Or if you want to get a Double value instead on an Integer you can use NextDouble(), then you'll receive a "random" number between 0.0 and 1.0, you can't change the minimal value and maximal value when using NextDouble. So NextDouble simply looks like this:
[highlight=VB.NET] r.NextDouble()[/highlight]
This was it, remember that none of the random numbers you'll get will be completely random no matter how many times you're using the number generators to get only one number, but they are very often "random enough". Hope you enjoyed this tutorial![]()
Very cool! +rep
Useful+rep
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks