# Thread: generate 4 - digits pin of same characters or digits Thanks: 0 Likes: 0

1. ## Re: generate 4 - digits pin of same characters or digits

Originally Posted by shknbk2

Code:
```Function ReturnPin() As String
Dim i As Integer, i2 As Integer
Randomize
i = Rnd * 10
i2 = Rnd * 9
Select Case i
Case 10
i = Rnd * 6
ReturnPin = i & i + 1 & i + 2 & i + 3
Case Else
ReturnPin = i & i & i2 & i2
End Select
End Function```
Hahaha

It would have taken me millennia to figure this logic out!!

Working very great

Now all questions exhausted.

Have a great time

2. ## Re: generate 4 - digits pin of same characters or digits

You might be interested in this alternative construction for shknbk2's function (Message #10 )...
Code:
```Function ReturnPin() As String
Dim i As Integer, i2 As Integer
i = [RANDBETWEEN(0,10)]
If i < 10 Then i2 = Evaluate("MID(""0123456789"",RANDBETWEEN(1,9),1)")
ReturnPin = Evaluate("IF(" & i & "<10,TEXT(" & i & i & i2 & i2 & ",""0000""),MID(""0123456789"",RANDBETWEEN(1,6),4))")
End Function```
In another thread where you were testing the PIN's, you indicated that all four digits being the same was an invalid PIN. If you need this function to not return such numbers, then use this version of my function instead...
Code:
```Function ReturnPin() As String
Dim i As Integer, i2 As Integer
i = [RANDBETWEEN(0,10)]
If i < 10 Then i2 = Evaluate("MID(SUBSTITUTE(""0123456789""," & i & ",""""),RANDBETWEEN(1,9),1)")
ReturnPin = Evaluate("IF(" & i & "<10,TEXT(" & i & i & i2 & i2 & ",""0000""),MID(""0123456789"",RANDBETWEEN(1,6),4))")
End Function```

3. ## Re: generate 4 - digits pin of same characters or digits

Okay cool. For now I am interested in the first part of the code you provided. I will later see what I can do with the second part. For now I need those same numbers in place

It's seems to randomly select the various numbers cooler. Almost even for the "abcd", "aaaa", "aabb" formats.

Shknbk's version favoured the "aabb" than the others

PS.
Did you intentionally leave out the

Randomize key word?

4. ## Re: generate 4 - digits pin of same characters or digits

Originally Posted by kelly mort
PS.
Did you intentionally leave out the

Randomize key word?
Yes... you only need the Randomize statement when you use VB's Rnd function... I used Excel's RANDBETWEEN function instead which does not require the Randomize statement.

Okay

## User Tag List

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•