Forum Moderators: DixonJones

Message Too Old, No Replies

Random Tracking IDs

My Random Id ... isn't so random :(

         

webdevsf

2:23 pm on Apr 20, 2004 (gmt 0)

10+ Year Member



Hi,

I've been using a 20 character random alpha numeric cookie to track unique visitors for the past few months. My estimation was that there were 36^20 combinations (26 alpha + 10 numeric, with 20 characters).

So that's 13,367,494,538,843,734,067,838,845,976,576 (13 gajillion) combinations. I thought i was safe to use a generate a number like this for each visitor.

But it turns out, no, these numbers are not even close to unique. Many visitors each day end up with the same "random" codes. They happen at different times of the day - i thought perhaps it was a problem with the random function in asp?

What the heck? Where's my math wrong here? I suppose i'll go to a GUID - but is that going to work?

Thx,
WDSF

john_k

2:27 pm on Apr 20, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you already have a function to crank out your random string, then just adjust it a bit. Have it extract the date and time using the year, month, day, hour, minute, and second functions. String all of those together and then add a random component onto one end.

ukgimp

2:33 pm on Apr 20, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



what about getting a stimestamp and then md5()'ing it. So if by chance you have two at the same time you know it will be different due to the md5. Plus people will find it difficult to mess with it.

karmov

7:57 pm on Apr 20, 2004 (gmt 0)

10+ Year Member



I'm not sure if this is the problem, but most every random function I've ever met requires a seed. A seed is something you give the function to base it's random number generation on, otherwise people could generate the exact same sequence of random number as everyone else. Without a seed, this is exactly what happens so when a program is run it will generate seemlingly random numbers, but they will be exactly the same "random" numbers as the last time you ran it and the next time you will run it.

The Unix timestamp was a practical seed when I dealt with these kinds of things. I'm sure asp has some kind of numerical date representation that you could use if this is in fact the problem.

Not sure if I've explained this well or not... If you have more questions, ask away.

dcrombie

10:30 am on Apr 21, 2004 (gmt 0)



PHP has the uniqid [php.net] function for this.