Thanks:  0
Likes:  0
Dislikes:  0

# Thread: Need a function to generate a 4 to 8 digit numbers

1. ## Need a function to generate a 4 to 8 digit numbers

Hi,

I need to a function to generate 4 to 8 digit numbers randomly, can you please help me in that?

thanks.

2. ## Re: Need a function to generate a 4 to 8 digit num

I am sure below function will work. Just reply if it works

msgbox Random (RandomNumber(1,5))

Function Random (digit)

Select Case digit
Case 1 Random = (Cstr(RandomNumber (0,9) )+Cstr(RandomNumber (0,9) )+Cstr(RandomNumber (0,9) )+Cstr(RandomNumber (0,9) ))
Case 2 Random = Cstr(RandomNumber (0,9) )+Cstr(RandomNumber (0,9) )+Cstr(RandomNumber (0,9) )+Cstr(RandomNumber (0,9) )+Cstr(RandomNumber (0,9) )
Case 3 Random = Cstr(RandomNumber (0,9) )+Cstr(RandomNumber (0,9) )+Cstr(RandomNumber (0,9) )+Cstr(RandomNumber (0,9) )+Cstr(RandomNumber (0,9) )+Cstr(RandomNumber (0,9) )
Case 4 Random = Cstr(RandomNumber (0,9) )+Cstr(RandomNumber (0,9) )+Cstr(RandomNumber (0,9) )+Cstr(RandomNumber (0,9) )+Cstr(RandomNumber (0,9) )+Cstr(RandomNumber (0,9) )+Cstr(RandomNumber (0,9) )
Case 5 Random = Cstr(RandomNumber (0,9) )+Cstr(RandomNumber (0,9) )+Cstr(RandomNumber (0,9) )+Cstr(RandomNumber (0,9) )+Cstr(RandomNumber (0,9) )+Cstr(RandomNumber (0,9) )+Cstr(RandomNumber (0,9) )+Cstr(RandomNumber (0,9) )
End Select
End Function

3. ## Re: Need a function to generate a 4 to 8 digit num

Msgbox RandomNumber.value(1000,99999999)

4. ## Re: Need a function to generate a 4 to 8 digit num

Do Until Response = vbNo
x = int((99999999-1000+1)*Rnd+1000)
msgbox x
Response = MsgBox ("Roll again? ", vbYesNo)

Loop

5. ## Re: Need a function to generate a 4 to 8 digit num

hey that preety well..

but what abt the number 0001 ?? and 0123?? aren't they 4 digit numbers ??

I think someone has posted a question coz he is facing the problem, otherwise it's a simple function. And I am sure he knows that already.

6. ## Re: Need a function to generate a 4 to 8 digit num

[ QUOTE ]
hey that preety well..

but what abt the number 0001 ?? and 0123?? aren't they 4 digit numbers ??

I think someone has posted a question coz he is facing the problem, otherwise it's a simple function. And I am sure he knows that already.

[/ QUOTE ]

Do Until Response = vbNo
x = int((99999999-0000+1)*Rnd+0000)
a = int((9-0+1)*Rnd+0)
b = int((9-0+1)*Rnd+0)
c = int((9-0+1)*Rnd+0)

if len(x) &gt;= 4 then
msgbox x
Else if len(x) = 1 then
x = x&amp;a&amp;b&amp;c
msgbox x
Else if len(x) = 2 then
x = x&amp;a&amp;b
msgbox x
Else if len(x) = 3 then
x = x&amp;a
msgbox x
End if
End if
End if
End if
Response = MsgBox ("Roll again? ", vbYesNo)

Loop

7. ## Re: Need a function to generate a 4 to 8 digit num

Hey That's good too...

8. ## Re: Need a function to generate a 4 to 8 digit num

Generate 100 random numbers between 4 and 8 digits long

<font class="small">Code:</font><hr /><pre>
Sub GenerateRndNumber()
Dim strRandNum, strAggregate, iMonth, iDay, iYear, iHour, iMinute, iSecond, iRndSeed, iIndex

For i = 1 to 100
iMonth = Month(Now)
iDay = Day(Now)
iYear = Year(Now)
iHour = Hour(Now)
iMinute = Minute(Now)
iSecond = Second(Now)
iRndSeed = iSecond&amp;iMinute&amp;iHour&amp;iDay&amp;iMonth& amp;iYear
Randomize iRndSeed
For iIndex = 1 to Int((8 - 4 + 1) * Rnd + 4)
strAggregate = Int((9 - 0 + 1) * Rnd +0)
strRandNum = strRandNum&amp;strAggregate
Next
Print strRandNum
strRandNum = ""
strAggregate = ""
Next
End Sub
</pre><hr />

9. ## Re: Need a function to generate a 4 to 8 digit num

[ QUOTE ]

but what abt the number 0001 ?? and 0123?? aren't they 4 digit numbers ??

[/ QUOTE ]

Technically they are not 4 digit numbers. They are 4 digit strings

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•
Search Engine Optimisation provided by DragonByte SEO v2.0.36 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.