Dim intMax as Integer

dim intRandom as Integer

itMax = 1000

intRandom = intMax * Rnd ' random integer between 0 and 1000

If yu want a randome nyumber between 500 and 1500, then simply add 500 to intRandom above

Arthur Wood

Solved

Posted on 2002-07-06

what is the BASIC code for generate a random number with certain length?

Thanks

Thanks

5 Comments

Dim intMax as Integer

dim intRandom as Integer

itMax = 1000

intRandom = intMax * Rnd ' random integer between 0 and 1000

If yu want a randome nyumber between 500 and 1500, then simply add 500 to intRandom above

Arthur Wood

Public Function GenerateRandomNum() As Integer

Randomize ' Initialize random-number generator.

GenerateRandomNum = Int((10 * Rnd) + 1)

End Function

' Calling RandomNumber Function in

'Command Button Click Event

Private Sub Command1_Click()

Dim intRNum As Integer

intRNum = GenerateRandomNum

MsgBox intRNum

End Sub

CHeers

Dim intCode As Integer

Dim intStart As Integer

Dim intEndPlusOne As Integer

Randomize

intStart = 1000

intEndPlusOne = 10000

' intCode will be between intStart and intEndPlusOne-1

' which is 1000 to 9999

intCode = Int((intEndPlusOne - intStart + 1) * Rnd + intStart)

hongjun

Public Function Rand(ByVal Minimum As Double, ByVal Maximum As Double, Optional IntegerOnly As Boolean = True) As Double

'

' Generate a number that falls between the Minimum and Maximum

'

Dim N As Double

Randomize

N = (Rnd * (Maximum - Minimum)) + Minimum

If IntegerOnly Then

Rand = Int(N)

Else

Rand = N

End If

End Function

Option Explicit

Private Sub Command1_Click()

MsgBox fRandNumString(40)

End Sub

Public Function fRandNumString(ByVal lngLen As Long) As String

Dim i As Long

Randomize

fRandNumString = Space(lngLen)

For i = 1 To lngLen

Mid$(fRandNumString, i, 1) = CStr(Int(10 * Rnd))

Next i

End Function

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Title | # Comments | Views | Activity |
---|---|---|---|

using Access | 8 | 59 | |

Add and format columns in vb6 | 7 | 47 | |

Protecting vb6 & .Net code Obfuscation | 18 | 95 | |

Can we place a tooltip on the actual vb6 form | 5 | 35 |

Join the community of 500,000 technology professionals and ask your questions.

Connect with top rated Experts

**12** Experts available now in Live!