Picking Random Numbers

D

Dwight

I have been tasked with coming up with a form that will automatically assign
a random number to an individual with the click of a button.

The number is 7 digits in length.
Each element of the number can range from 1 to 9.

Something simular to a lottery number picker.

Does anyone have any sample code that I can refer to?

Thanks in advance!

Dwight
 
D

Douglas J. Steele

Function GenerateRandomNumber() As String
Dim lngLoop As Long
Dim strResult As String

Randomize

For lngLoop = 1 To 7
strResult = strResult & Format((Int(9 * Rnd) + 1), "0")
Next lngLoop

GenerateRandomNumber = strResult

End Function

If what you want is the more usual digits ranging from 0 to 9, use

Function GenerateRandomNumber() As String
Dim lngLoop As Long
Dim strResult As String

Randomize

For lngLoop = 1 To 7
strResult = strResult & Format(Int(10 * Rnd), "0")
Next lngLoop

GenerateRandomNumber = strResult

End Function
 
K

KARL DEWEY

Try this --
Round(Rnd() * 9999999, 0) + 1

If your table has an Autonumber as primary key then try this --
Right(Left((.46345679/[aaa])*100000,15),7)
 
D

Dwight

Thank you!

Douglas J. Steele said:
Function GenerateRandomNumber() As String
Dim lngLoop As Long
Dim strResult As String

Randomize

For lngLoop = 1 To 7
strResult = strResult & Format((Int(9 * Rnd) + 1), "0")
Next lngLoop

GenerateRandomNumber = strResult

End Function

If what you want is the more usual digits ranging from 0 to 9, use

Function GenerateRandomNumber() As String
Dim lngLoop As Long
Dim strResult As String

Randomize

For lngLoop = 1 To 7
strResult = strResult & Format(Int(10 * Rnd), "0")
Next lngLoop

GenerateRandomNumber = strResult

End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top