【VB】亂數應用

應用一:
微軟建議格式
randomValue = CInt(Math.Floor((upperbound - lowerbound + 1) * Rnd())) + lowerbound
在這裡,upperbound是範圍的最大數字,lowerbound則是範圍的最小數字。
Ex: 取1~1000中的亂數(13、123..等),randomValue 取得亂數
randomValue = CInt(Math.Floor((1000 - 1 + 1) * Rnd())) + 1

應用二:
Function GetRandomNumberString(ByVal int_NumberLength As Integer) As String

        Dim str_Number As String = String.Empty
        Dim theRandomNumber As New Random()
        For int_index As Integer = 0 To int_NumberLength - 1
            str_Number += theRandomNumber.[Next](10).ToString()
        Next
        Return str_Number
 End Function

Ex: 5位數字的驗證碼,數字前面不足位數補0(00123、12345..等),str_ValidateCode  取得亂數
Dim str_ValidateCode As String = GetRandomNumberString(5)

留言

這個網誌中的熱門文章

【ASP】日期轉換(西元<-->民國)

【VB】使用NPOI元件來匯出Excel--DataTableToExcel

【SQL】符號切割字串變成多欄