Random Number generation In Visual Basic 2005 Beta 2 Express
Random Number generation In Visual Basic 2005 Beta 2 Express
Hi. Can anyone tell me how to generate a random number between 2 other's I have found a function for it in the object browser:
System.Random.Next(integer, integer)
but I can't access it in my code. Any help would be wonderful.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Thanks,
The Digital Pioneer
Random Number generation In Visual Basic 2005 Beta 2 Express
Marie 5
Xeric Systems Ltd
So just change your first line to this:
Dim rand As New Random
Onega
from sample of MSDN:
========================
Sub BothBoundsRandoms( _
seed As Integer, lower As Integer, upper As Integer )
Console.WriteLine( vbCrLf & _
"Random object, seed = {0}, lower = {1}, " & _
"upper = {2}:", seed, lower, upper )
Dim randObj As New Random( seed )
' Generate six random integers from the lower to
' upper bounds.
Dim j As Integer
For j = 0 To 5
Console.Write( "{0,11} ", _
randObj.Next( lower, upper ) )
Next j
Console.WriteLine( )
End Sub
========================
cdznr
Console.ReadLine()
This will prevent the Console from just opening and closing, but instead will wait for you to hit the "enter" key. I am able to build a Console App and run it, so it "seems" to be working fine.
As for the null reference exception, I wouldn't want to ignore it. If you post the code that is giving you trouble, I can explain what is going on.
lkld999
Console.WriteLine("SomeString")
It would display "SomeString" on the console.
To use the Random Class just do something like the following.
Dim randomNumber As New Random
This will create a new instance of the Random class. You can then generate a random number between two numbers by doing.
randomNumber.Next(lowerValue, upperValue)
This would then create a random number between lowerValue and upperValue.
All together (Using a Label called lblDisplay):
Dim lowerValue As Integer = 5
Dim upperValue As Integer = 100
Dim createdRandom As Integer
Dim randomNumber As New Random
createdRandom = randomNumber.Next(lowerValue, upperValue)
lblDisplay.Text = createdRandom
Hope this helps!
Oh, if you do "Start >> Run" type in "cmd" and hit OK, that is the console..
Nick The Newbie
Ron L
Dim rand As Random
Dim createdrand As Integer
createdrand = rand.Next(1, 100)
Pix
amila
Janine Zhang
Sorry, but that really doesn't make much sense to me. I am rather new to VB. I have only ever used Visual Basic 2005 Express, and therefore can't have much knowledge yet. After all, I am The Digital Pioneer. Still learning. So can that be put in simpler terms
By the way, I see that there is heavy use of the console there. What exactly is the console
Thanks,
The Digital Pioneer