Forum Moderators: coopster

Message Too Old, No Replies

Randomizing

         

RussellC

4:16 pm on Aug 25, 2004 (gmt 0)

10+ Year Member



I have been trying to think of a way to make my client's job numbers look more random to our clients but still have some kind of contstant so I can pre print labels with the job numbers on them.

Currently they are entered into the DB and assigned a job number that is just an auto-incremented ID. I have labels printed with these auto incrememented numbers to put on the jobs as they come in (service company).

I am trying to figure out if there is some kind of constant math formula that I can use on the auto-incremented ID so the number will not repeat itself again, but at the same time not be random so I can pre print labels with the numbers on them.

I have thought of:

1 - Take the square root of the number - but them what happens if I get a perfect square

2 - Use the sin cos or tangent of the number - I think these will repeat eventually though

3 - take a hash vaule and remove the alpha characters - not sure if this will repeat or not

If anyone has any other ideas, let me know?

Thanks

RussellC

8:02 pm on Aug 25, 2004 (gmt 0)

10+ Year Member



OK, I guess I was a bit confusing. I tried something like this and it creates random numbers from my ordered numbers by using the logarithm, but I do not know if it will ever hit the same number again. Is there a way to test this?

<?php
$x = 4194;
for ($x = 4194; $x <= 4500; $x += 1) {
$jobnumber = floor(log($x) * 1000000000);
$jobnumber2 = substr("$jobnumber", 4);
echo("$x - $jobnumber2<br />");
}
?>

dmorison

8:11 pm on Aug 25, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I would use a second database table to do this. Create a table "map" with two fields:

id (int 11,auto-increment, etc. etc.)
foo (int 11,indexed)

Then, knock up a PHP script to execute the following query 1 Million (or however many you want) times:

INSERT INTO map (foo) VALUES (RAND())

Then, to get your unique ID's that are not sequential but guaranteed never to repeat again; simply use the "id" from this map table selected using the following query:

SELECT id FROM map ORDER BY foo,id LIMIT $x,1

Where $x is the auto-increment ID from your other table.

RussellC

8:44 pm on Aug 25, 2004 (gmt 0)

10+ Year Member



Interesting idea, I will definately try that way as well. The more and more I try to do this the more confusing it gets. I wish there was a way to embed the "sequential" jobnumber into the new "random looking number" and make it all automated and still be able to tell which number would be next. Am I asking for too much?

httpwebwitch

9:15 pm on Aug 25, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



you can get random-looking numbers by reversing the digits and stringing up the ascii values of each digit. If the number is less than 10, add a zero

00 = 4848
01 = 4948
02 = 5048

12 = 5049
13 = 5149
14 = 5249

130 = 485149
131 = 495149
132 = 505149

they're easy to dissemble and translate back into the original numbers.

Or try this.
take your number and divide it by 7.
6 out of 7 integers will produce a 6-digit repeating fraction. chop off the repeating fraction. take the number of decimals you chopped at and make this your first digit.

my number is 411.
/7 = 58.714285714285714285714285...
chop = 58.714285
remove decimal = 58714285

Then to get your number back, divide by 1000000 to get the decimal back. multiply by 7 and round it back to the integer. Because you were accurate to 6 decimals, the product will be off by 0.00001 or less.

For numbers that are evenly divisble by 7 (like 413), your dividend will be a low integer (like 59). to obfuscate this, use the ascii value method described above.

Make it as complicated as possible, and you'll be guaranteed job security for as long as it's in use :-)

httpwebwitch

9:19 pm on Aug 25, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



for an ideal random-to-ordered map, generate an array from 1 to n.
loop and assign each array element a random value.
sort the array by value,
walk through the keys and push them into a new array with fresh keys.

bascially it's like getting a deck of n cards, shuffling them, and noting the position of each card for future reference. number 1342 is always at 75422. number 882 is always at 402. and so on.

put it all in a database, and hope to heck you never lose it.