How to select random two consecutive digits from a number in VB.NET

1 Answer

0 votes
Imports System

Module Program
    Function GetRandomTwoDigits(num As Integer) As String
        Dim s As String = num.ToString()

        If s.Length < 2 Then
            Return "Error: number must have at least 2 digits"
        End If

        Dim rand As New Random()
        Dim start As Integer = rand.Next(s.Length - 1) ' [0 .. Length-2]

        Return s.Substring(start, 2)
    End Function

    Sub Main()
        Dim num As Integer = 123456
        Dim randomTwo As String = GetRandomTwoDigits(num)

        Console.WriteLine("Random two digits: " & randomTwo)
    End Sub
End Module


' run:
'
' Random two digits: 34
'

 



answered 3 days ago by avibootz
...