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.Smile



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Thanks,
The Digital Pioneer


Answer this question

Random Number generation In Visual Basic 2005 Beta 2 Express

  • Uglydragon79

    <cough>

    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

  • ISW

    OK, thanks a whole lot. That really helped.Big Smile Although the console in code (when compiled) doesn't look anything like the Command Console when run inside the IDE, and outside of the IDE, it doesn't do anything. Is that a bug in the 2005 Beta 2
  • koosha

    The example above uses the console instead of a windows form.  Using the console can be an easier way of giving examples.  So where it says:


        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!  Big Smile

    Oh, if you do "Start >> Run" type in "cmd" and hit OK, that is the console..



  • JR

    lol same here! Could someone please explain this...

  • mam6669

    Uh-oh. Bad news.Sad More trouble. I used the code (modified slightly) above, but it said that I was using 'randomnumber' before a value had been given to it, and that a null reference exception could occur at runtime. Can that simply be ignored, or do I have to do it another way

  • TheSun

    All right, sure.



    Dim rand As Random
    Dim createdrand As Integer
    createdrand = rand.Next(1, 100)

     



  • Siggi Bjarnason

    You are just missing New.  As of right now, you have created a variable called rand, that can hold a Random object, but have not created an instance of that object.  Therefore your rand variable holds null.  Hence the exception.  New actually creates an instance of an object.

    So just change your first line to this:


    Dim rand As New Random
     





  • AndyL

    For your console problem try adding this line to the very end of your code:


    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.

  • labtech

    hello...
    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
    ========================

  • breed

    OK, thanks a million. That did it.Smile

  • Random Number generation In Visual Basic 2005 Beta 2 Express