Imports System
Module Program
' Reuse a single Random instance across calls.
' Creating new instances inside a method rapidly can seed them with the same timestamp,
' leading to identical repeated numbers.
Private ReadOnly Rng As New Random()
' Explicit inclusive boundaries for an 8-digit positive integer
Private Const Min8Digit As Integer = 10000000
Private Const Max8Digit As Integer = 99999999
Sub Main()
' 1. Generate an 8-digit mathematical integer [10000000 to 99999999]
Dim number As Integer = Generate8DigitNumber()
Console.WriteLine($"Generated 8-digit integer: {number}")
' 2. Generate an 8-character string code [00000000 to 99999999] preserving leading zeros
Dim codeStr As String = Generate8DigitCode()
Console.WriteLine($"Generated 8-digit code string: {codeStr}")
End Sub
''' <summary>
''' Generates a random 8-digit positive integer between 10,000,000 and 99,999,999 (inclusive).
''' Guaranteed to start with a non-zero digit (1-9).
''' </summary>
''' <returns>An 8-digit Integer value.</returns>
Public Function Generate8DigitNumber() As Integer
' Random.Next(min, max) excludes the max bound.
' Passing (Max8Digit + 1) ensures 99,999,999 is reachable.
Return Rng.Next(Min8Digit, Max8Digit + 1)
End Function
''' <summary>
''' Generates an 8-character numeric string code between "00000000" and "99999999".
''' Preserves leading zeros using format string "D8", suitable for OTPs or PINs.
''' </summary>
''' <returns>An 8-character numeric String.</returns>
Public Function Generate8DigitCode() As String
' Range: 0 to 99,999,999 (upper bound 100,000,000 is excluded)
Dim rawValue As Integer = Rng.Next(0, 100000000)
' Format "D8" pads the number with leading zeros up to 8 digits
Return rawValue.ToString("D8")
End Function
End Module
'
' run:
'
' Generated 8-digit integer: 74280376
' Generated 8-digit code string: 95911329
'