Source code for Issue Number 59

Copyright 1997-2000 by C&D Programming Corp. All Rights Reserved. Source code may not be reproduced except for use in a compiled executable. All rights reserved. If you would like to reprint any or all of this code please email us at info@codeoftheweek.com

Code of the Week Home


Source Code

Just paste this source code into a module called basRandom and include it in your project.

'----------------------------------------------------------------------
'
'   Module Name:    basRandom
'   Written By:     C&D Programming Corp.
'   Create Date:    10/98
'   Copyright:      Copyright 1998 by C&D Programming Corp.  Source
'                   code may not be reproduced except for use in a
'                   compiled executable.  All rights reserved.  If
'                   you would like to reprint any or all of this
'                   code please email us at info@codeoftheweek.com
'----------------------------------------------------------------------
Option Explicit

Public Enum eCharacterTypes
    ctUpper         ' upper case only
    ctLower         ' lower case only
    ctMixed         ' mixed case
End Enum

Function RandomNumberBetween(lMin As Long, lMax As Long) As Long
    RandomNumberBetween = (Rnd * (lMax - lMin)) + lMin
End Function

Function RandomText(lBytes As Long, _
                        Optional eChType As eCharacterTypes, _
                        Optional bInsertWordBreaks As Boolean = True) As String
    Dim sText As String
    Dim lPosCount As Long

    Randomize
    sText = String$(lBytes, " ")
    Select Case eChType
        Case ctUpper
            ' do loop to get upper case text.  65 is capital A, 90 is capital Z
            For lPosCount = 1 To lBytes
                Mid$(sText, lPosCount, 1) = Chr$(RandomNumberBetween(65, 90))
            Next
        Case ctLower
            ' do loop to get lower case text
            ' 97 is lowercase A, 122 is lowercase Z
            For lPosCount = 1 To lBytes
                Mid$(sText, lPosCount, 1) = Chr$(RandomNumberBetween(97, 122))
            Next
        Case ctMixed
            ' do loop to get mixed case text.  The logic is to generate a
            ' random number between 1 and 2.  If the number is 2 it
            ' generates a lowercase character otherwise it generates an
            ' uppercase value.
            For lPosCount = 1 To lBytes
                If RandomNumberBetween(1, 2) = 2 Then
                    Mid$(sText, lPosCount, 1) = Chr$(RandomNumberBetween(97, 122))
                Else
                    Mid$(sText, lPosCount, 1) = Chr$(RandomNumberBetween(65, 90))
                End If
            Next
    End Select

    ' If the user wants word breaks this routine will
    ' generate a random number from 2 to 10 to create
    ' words that are 2 to 10 characters long by inserting
    ' a blank at each random position
    If bInsertWordBreaks Then
        lPosCount = RandomNumberBetween(2, 10)
        While lPosCount < lBytes
            Mid$(sText, lPosCount, 1) = " "
            lPosCount = RandomNumberBetween(2, 10) + lPosCount
        Wend
    End If

    RandomText = sText
End Function