RE: [PHP-WIN] Random Code

From: Date: Tue, 23 Jan 2001 19:55:26 +0000
Subject: RE: [PHP-WIN] Random Code
Groups: php.windows 
Request: Send a blank email to php-windows+get-5170@lists.php.net to get a copy of this message
Indeed a very interesting question you put forward (and probably OOT too :-) In programming there does not exist "The Best Way". "Best" always depends on what you consider to be important and less important - that's why programmers most often writes same piece of code in different ways, and that's why one should be considerate about to think an extra time before one classifies some other programmers code as "crap shit". :) (For ex there might be a reasons like "The Best Way" why M$ made there OS code as they did - though it hard for use normal mortal none-devine programmers to perceive it... :) There exists a class of 1-1 functions that behaves "randomly", you can uses one of these. They are well know from analytic math. In principle they work like this: in the beginning (x close to zero) the curve f(x) behave relatively nice like any other curve, but as one progress further away from zero (towards +oo) f(x) starts to get a shape that reminds of a truly random distributed curve, but f(x) is of course still functional dependent - that is, "FOR-EVERY(a) : IF f(a)=Y1 AND f(a)=Y2 THEN Y1=Y2", read: for each time you execute the function with an input, it will yield the same output as the previous calculation with the very same input. In other words, the function result is determent in advance (that is not randomly generated) - but still: it is bloody hard to predict its output in without actually having done the calculation in advance. ;) The "only" thing you then have to do is two things: 1) create a mapping function for the output, for ex: 1 == 'AAAA-0000', 2 == 'AAAA-0001', etc, etc, or what ever way you like it to be. 2) keep track of "used" numbers. One simple, but naive, approach to 2) would be to simply starting at a specific predetermined number (x-value), and then just increment x with a fixed factor, for ex 0,1 (= 0.1, in U.S. notation), or Pi, or whatever-you-feel-for, whenever you need to produce a new number However, this does not guarantee uniqueness, which means that you need to check the current output value against all previous output values. This is made simple by a brute force algorithm which, obviously, scans the previous number whether an output of this value has been or not has been produced before. This is the "best" way if you want to write a simple algorithm, but it is not the best way considering CPU load on the system. For this, there exist hashing techniques to keep track of large number of values, this might be fast finders of used numbers. This could be considered "best" from an CPU-minimization approach, but the programming gets a little bit more tricky. But happily enough, as this forum implicitly is about databases, one can "cheat" and store the numbers in the database(!), index the table that stores the values, and then get rid of the problem to have to write the hashing algorithms - since it already done in the DBMS. >-----Original Message----- >From: Sigurd Magnusson [mailto:phobo@paradise.net.nz] >Sent: Tuesday, January 23, 2001 9:59 AM >To: php-windows@lists.php.net >Subject: [PHP-WIN] Random Code > > >Whats the best way to make a completely random, unique code in >the format "ABCD-1234" where each of the 8 characters can be >alphanumerc; 0-9,A-Z... I thought about generating a 36^4 >number, converting to to base '36', and repeating the process. >What's a good way to ensure the code is not repeated, though? > >Siggy >

« previous php.windows (#5170) next »