How to generate an 8 digits random number in VB.NET

2 Answers

0 votes
Imports System

Module Module1

    Sub Main()

        Const N As Integer = 20

        ' Reuse a single instance of Random to avoid repeated initialization
        Dim rng As New Random()

        ' Loop from 0 to N - 1 (exactly 20 iterations)
        For i As Integer = 0 To N - 1
            ' Next(min, max) excludes max, so 100000000 allows results up to 99999999
            Dim randomNum As Integer = rng.Next(10000000, 100000000)
            Console.WriteLine(randomNum)
        Next

    End Sub

End Module
                 
                     
'
' run:
'
' 74852357
' 34151816
' 25431339
' 28361898
' 97645021
' 84116087
' 20694223
' 95221690
' 67306231
' 49284918
' 51704380
' 83030577
' 57383925
' 48362398
' 81845508
' 47062332
' 17410230
' 31147746
' 80484398
' 97456092
' 

 



answered Apr 27, 2016 by avibootz
edited Oct 2 by avibootz
0 votes
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
' 

 



answered Oct 2 by avibootz
...