homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

Generating HARD-TO-GUESS random numbers
a naguib 2000

 11:43 pm on Jun 15, 2009 (gmt 0)


Using PHP I want to generate a unique random 14 digits HARD-TO-GUESS number that will fill about 100000 rows in a MySQL table. It's easy to use the rand function of PHP to generate a random unique 14 digits number. The problem actually lies in the word HARD-TO-GUESS.

I think HARD-TO-GUESS actually means that for every single number there must be at least 3 nonadjacent digits different than any other number previously generated or stored.
1) 11111111111111 accepted
2) 12112112111111 accepted
3) 12112112121212 accepted
4) 11111111111110 not accepted, only 1 different
5) 11111111111122 not accepted, only 2 and adjacent
6) 21111111111122 not accepted, 2 digits adjacent
7) 11111111111222 not accepted, 3 digits adjacent

I know it's a complex task, but I think it can be a very helpful application for everybody.


Baruch Menachem

 12:38 am on Jun 16, 2009 (gmt 0)

Can't you just use a regular expression string to test it? They have imposed this kind of test where I used to work for passwords, IE, no characters or number in sequential order or in series. put it in a small loop, repeat until ! regex_fail.


 12:57 am on Jun 16, 2009 (gmt 0)

Actually, this task is easier than it sounds as long as you DONT use the built in random number generator of php (or any other application languages). These functions are rarely truely random because they are not seeded properly - the problem is generally not evident, until you try and do something like you suggest.

A 14 digit number is so big that few numbers should have the 3 adjacent number problem.

There are several API's online that give you access to true random number generators. Look for one of those - that will allow you to generate truely random numbers less likely to meet your reject criteria.


 1:05 pm on Jun 16, 2009 (gmt 0)

hehe the point of random is it isn't predictable, rand in php is significantly random, that doesn't mean you won't get the same number twice in a row or that you won't get sequential numbers.

it's random

if you want to enforce some rules then what you are trying to do is actually reduce the randomness and enforce some type of rules

you could generate it number by number and do tests as you go
you could just use regex to test for the specific rules you would like to enforce

it's not overly complicated, you just need to decide on your rules, generate a number, if it passes the rules insert it, if not throw it away and start over

a naguib 2000

 2:52 am on Jun 17, 2009 (gmt 0)

Thank you all for your replies..

@Baruch Menachem:
Actually, I guess some of the rules can be made with regex not all of them. The problem is I'm newbie to regex..

I think either it is seeded properly or not, I have to check the results with some rules that don't make similar numbers to be stored. I've searched the net but I think that most of the results were not very in depth.

This is exactly what I want to do: Reduce the randomness to make it less predictable. Anyway, I will need help with regex.

For all:
New rules for validating a single number:
1) Maximum repetition for a typical number is 3.
2) Maximum adjacent repetition for a typical number is 2.
3) Most significant digit must not start with 0.

Rules for validating a number with respect to previously generated:
1) At least 3 nonadjacent digits must be different from any other number. Examples are in my first post.
2) The difference between any pair of generated number must not be multiple of 10, (i.e, 10,100,1000,10000,..,ect).


 3:15 am on Jun 17, 2009 (gmt 0)

Why do you think you want this? What exactly are you trying to do?

a naguib 2000

 4:32 am on Jun 17, 2009 (gmt 0)

I'm trying to make an application for generating something like prepaid card numbers, which can be used for many services.


 3:11 pm on Jun 17, 2009 (gmt 0)

so start by dealing with your first three

do you have ideas how to address those?

you don't necessarily need regex, you could also generate the number 1 digit at a time, though this isn't the only answer

take a shot and see what you come up with

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved