Random ID/Number Generator in PHP -
I am creating a list of "agent ID" in my database with the following requirements:
- ID must be 9 digits (numeric only)
- The ID can not be more than 3 out of the same number.
- The same number can consist of more than 2 consecutive digits (i.e. 887766551; 888).
So far my share is solid 1 down, but I My code is struggling with 2 and 3 is below.
Function vs. RandomGatino () {srand ((double) microtim () * 1000000); $ Random_agtno = Rand (1000000000000000000); Return $ random_agtno; } // use $ NEWAGTNO = createRandomAGTNO ();
Any ideas?
- Unless your PHP is very old , You probably do not need it, unless you want to completely cover the security of your random number. Again the seeds are RNG at all, just as PHP seeds give it to you on startup and there are very few cases where you need to replace the seed with your own selection.
- If this is available to you, use it instead of
rand
instead ofmt_rand
. My example will usemt_rand
.
For the rest - you probably want a pretty clever mapping of numbers from a linear range, but instead force toughness. This is one of those things where, yes, theoretical upper limit is infinite at the time of walking, but is expected to run and is very small, so do not worry.
Function created by MrendagAgtono () {do {$ agt_no = mt_rand (100000000,900000000); $ Valid = true; If (preg_match ('/ (\ d \'), \ $ agt_no $ is valid = false; // same number three consecutive consecutive ('/ (\ / \ d). * * 1. * * 1? * * ?? 1 / ', $ agt_no] $ valid = incorrect; // string in the same number four times} while ($ valid === incorrect); return $ agt_no;}
Comments
Post a Comment