# How to generate 5-digit number using a phone keypad

This topic is 2236 days old. Please don't post here. Open a new topic instead.

## Recommended Posts

For all those who love math problems ;-)

Given a phone keypad as shown below:

123

456

789

0

Need to generate random 5 (or variable length) digit numbers with the following constraints:

1. Has to be done completely in FM

2. Each number has to be with in within one movement on the keypad including it's self. ie #2 could possibly be followed by the numbers 1,2,4,5,6,3. While #3 could possible be followed by the numbers 2,3,5,6.

3. Repetition of numbers are allowed as long as the same number is not repeated consecutively more then once-  12123, 44588,  78853 are all valid numbers, 98887 is not.

Any takers?

##### Share on other sites

Why are you asking? Is this a puzzle for us to solve or can you please show us a real case scenario where this would  be needed?

Edited by Rick Whitelaw

##### Share on other sites

Here is what I came up with, but I am sure there is a more concise way using advance math.

##### Share on other sites

4 hours ago, Tyra said:

For all those who love math problems ;-)

I am not sure I would necessarily look at this as a math problem. There are only 10 digits, so you could easily associate each digit with a string of digits that are allowed to follow it. Then look at the last generated digit and select a random digit from the string associated with it.

Still, since this was posed as a math challenge, you could look at it this way:

Take the last generated digit and calculate its row and column numbers in the keypad array. Next, generate a tentative random digit and calculate its row and column numbers. If the absolute differences between the rows and the columns are ≤ 1, accept the number. Otherwise keep trying.

To prevent consecutive triplicates, add a check for that before accepting the tentative digit.

##### Share on other sites

Comment, as in my example, I went your first route, but the difference, is that if the random digit is not one of the allowed associate digits, I loop until one is found. Didn't think of narrowing the random digit to one of the allowed ones. Will have to play with that. Thanks.

##### Share on other sites

I am not able to look at your file, but going with the first method the calculation would be something like:

```Let ( [
lastDigit = Right ( \$password ; 1 ) ;
string = Choose ( lastDigit ;
"0789" ;
"1245" ;
"213456" ;
"3256" ;
"412578" ;
"512346789" ;
"623589" ;
"74580" ;
"8456790" ;
"95680" ) ;
// eliminate consecutive triplicates
string = If ( lastDigit = Middle ( \$password ; Length ( \$password ) - 1 ; 1 ) ; Substitute ( string ; lastDigit ; "" ) ; string )
] ;
\$password & Middle ( string ; Int ( 1 + Random * Length ( string ) ) ; 1 )
)```

This would be used in a script like this:

```Set Variable [ \$password; Value:Int ( Random  * 10 ) ]
#
Loop
Set Variable [ \$password; Value:«the calculation above»]
End Loop
#

---

BTW, out of curiosity: why is it necessary to limit the movement on the keypad so?

Edited by comment
##### Share on other sites

For those of us who do care to look at this as a math problem, some of your constraints look like the kind of thing designed to enhance security, but some of them actually reduce security. Removing possible combinations, such as by not accepting passcodes with more than 2 repetitions of a single digit, reduces the entropy of the passcode — not good. On the other hand, anyone attempting to break a passcode by trying many combinations will try certain combinations more than others, and there's an understandable logic to defending against that by disallowing such combinations. But there are much better alternatives: increase the required wait time between failed passcode attempts, and require a reset after a certain number of failures. In the absence of other defenses, passcode restrictions don't do a whole lot to improve security; and in the presence of other defenses, the passcode restrictions make the system less secure. The only things you can do to make the passcode more secure from a mathematical perspective are to (1) make the passcode longer or (2) increase the size of the character set passcodes can use.

##### Share on other sites

5 hours ago, jbante said:

some of your constraints look like the kind of thing designed to enhance security,

Such thought would never have entered my mind.

##### Share on other sites

Comment, this is what I originally came up with:

Set Variable [ \$g ; Value: keypad::g_Length ]

Set Variable [ \$g ; Value: Case (  \$g="";5; \$g>10;10; \$g<1;5; \$g) ]

Set Field [ keypad::g_Length ; \$g ]

Loop

Set Variable [ \$n ; Value: 0 + Int ( Random * ( 9 - 0 + 1 ) ) ]

If [ \$l>1 and (Right ( \$o ; 1 ) = Middle ( \$o ; \$l - 1 ; 1 )) ]

Set Variable [ \$s ; Value: Right (\$o;1) ]

End If

Set Variable [ \$ls ; Value: Case ( \$n = 1;"1¶2¶5¶4"; \$n = 2;"1¶2¶3¶6¶5¶4"; \$n = 3;"2¶3¶6¶5"; \$n = 4;"1¶2¶45¶8¶7"; \$n = 5;"1¶2¶3¶4¶5¶6¶7¶8¶9"; \$n = 6;"2¶3¶5¶6¶8¶9"; \$n = 7;"4¶5¶7¶8¶0"; \$n = 8;"4¶5¶6¶7¶8¶9¶0"; \$n = 9;"5¶6¶8¶9¶0"; \$n = 0;"0¶7¶8¶9"; "") ]

Set Variable [ \$v ; Value: Right ( \$o;1) ]

If [ \$o = "" ]

Set Variable [ \$o ; Value: \$n ]

Else

If [ GetAsBoolean (Position ( ¶ & \$ls & ¶ ; ¶ & \$v & ¶ ; 1 ; 1 ) ) and (\$n ≠ \$s) ]

Set Variable [ \$o ; Value: \$o & \$n ]

End If

End If

Set Variable [ \$l ; Value: Length ( \$o ) ]

Set Variable [ \$s ; Value: "" ]

Exit Loop If [ \$l = \$g ]

End Loop

Set Field [ keypad::Code ; \$o ]

Yes, trying to kind of meet in the middle as far as security goes.. safe, but patterns more easily remembered.

##### Share on other sites

20 minutes ago, Tyra said:

but patterns more easily remembered.

Why would a password generated using these rules be any easier to remember than any other combination?

You also said that these are one-time passwords and users must immediately replace them with their own - so why would they need to remember them at all?

##### Share on other sites

This topic is 2236 days old. Please don't post here. Open a new topic instead.

## Create an account

Register a new account