Generating Specific random numbers

IN MSVB6 there is an inbuilt variable called "rnd" which I use to produce random numbers. But VB Express doesn't seem to have this. I don't what you use to produce random numbers . So basically I want to produce a random number between 2 values. For example I want to produce a random number between 4 and 20 which could be 15,9,6, etc...

Any idea how this can be done in VB Express
Thanks in advance



Answer this question

Generating Specific random numbers

  • MaxWeber

    Wow man thanks for the code snippet there it works fine but I get trouble when I use it like this:
    Dim objRandom As New System.Random(CType(System.DateTime.Now.Ticks Mod System.Int32.MaxValue, Integer))

    Public Function GetRandomNumber(Optional ByVal Low As Integer = 1, Optional ByVal High As Integer = 100) As Integer
    Return objRandom.Next(Low, High + 1)
    End Function

    Public Function GenerateRandomString(ByVal intMinLenght As Integer, ByVal intMaxLenght As Integer) As String
    Dim X As Long
    Dim strWord As String = ""

    For X = intMinLenght To intMaxLenght
    strWord = strWord & frmMain.lstChars.Items.Item(GetRandomNumber(0, frmMain.lstChars.Items.Count))
    Next

    Return strWord
    End Function

    I get null for the return value for some reason >_<


  • howardtr

    Some suggestions


    Dim objRandom As New System.Random(CType(System.DateTime.Now.Ticks Mod System.Int32.MaxValue, Integer))

    Public Function GetRandomNumber(Optional ByVal Low As Integer = 1, Optional ByVal High As Integer = 100) As Integer

    ' Returns a random number,
    ' between the optional Low and High parameters
    Return objRandom.Next(Low, High + 1)
    End Function

    Or


    Private Function GetRandomInteger(ByVal MinValue As Integer, ByVal MaxValue As Integer) As Integer
    Dim randomNumber As Integer

    Randomize()
    randomNumber = CInt(Int((MaxValue - MinValue + 1) * Rnd() + MinValue))

    Return randomNumber
    End Function



  • Sercan AYYILDIZ

    We'd have to know a little more, like what is this

    frmMain.lstChars.Items.Item



  • Nick Savoiu

    check out the random number class. It's well documented in help.



  • Generating Specific random numbers