php - generating a pseudo unique number(code) based on a sequence of numbers with no repetition within 4 digits -


i'm generating unique code don't want repeat within @ least 1000 consecutive numbers

this function. take number multiply number take last $length=5 digits before decimal point

function createpseudouniquestring($input,$length) {     return substr( intval($input*738510.93067),-$length) ; } 

is there way validate resulting numbers not repeat other testing possibilities?

is there alternative known not have repeating

you can design custom linear congruential generator generates random 5-digit numbers , guaranteed not repeat until has generated of them.

an lcg generates random numbers using following formula:

xn+1 = ((xn * a) + c) mod m

to generate 5-digit numbers m should 100000 (range of 0-99999).

to guarantee no repeats (a "full period") have select values , c using following criteria:

c , m relatively prime

a - 1 divisible prime factors of m

a - 1 multiple of 4 if m multiple of 4.

the prime factors of 100000 2 , 5, , it's divisible 4, multiple of 20 + 1 work suitable value of a, being careful not set large avoid integer overflows. c choose reasonably large prime number.

e.g: m = 100000, = 4781, c = 62873

set initial seed value x , generate each value previous 1 using $x = (($x*4781)+62873)%100000;

note can't use random number generator larger period , mod 100000 generated values, because though raw generated numbers larger-period rng don't repeat, doesn't guarantee numbers mod 100k won't.


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -