PHP: mt_rand()

Discussion in 'Web Design & Coding' started by vern, Aug 9, 2004.

  1. vern

    vern Dominus Political User Folding Team

    Messages:
    1,571
    Location:
    Minnesota, USA
    This is probably more of a math question than anything else, but does anyone know of the probability of mt_rand() coming up with two identical values? Note that I am using mt_rand to generate a unique identifier for an item, and store that in a database. As the number of items grows, the probability grows, but how long before this becomes a problem?
     
  2. Geffy

    Geffy Moderator Folding Team

    Messages:
    7,805
    Location:
    United Kingdom
    mt_rand() uses a Mersenne Twister which is supposed to result in a better random value, of course there are always chances of a collision with any random function, but if you seed the random number generator with mt_srand() with something which changes fairly periodically such as microtime() you might reduce the chances of a collision
     
  3. Glaanieboy

    Glaanieboy Moderator

    Messages:
    2,626
    Location:
    The Netherlands
    Depending on the size of the database, you could first generate a number, check it for existence in the db and if it exists, generate another number. I have done this on a website I am currently (almost finished) programming for a pizza website, and it's working quite well. Unless you have a db with 1000's of entries, it could be slow I guess. But that all depends on your hardware setup :)
     
  4. Khayman

    Khayman I'm sorry Hal... Political User Folding Team

    Messages:
    5,518
    Location:
    England
    why not just start with a number then keep adding one to it for each new item
     
  5. vern

    vern Dominus Political User Folding Team

    Messages:
    1,571
    Location:
    Minnesota, USA
    The database I'm talking about is fairly large and heavily used. ~211,000 records right now. I don't really want to start querying the db twice everytime a new entry is added.

    @Khayman: the database is already there, so no, I can't start from scratch and have the field auto-increment.

    Will have to look for some papers on this I guess as I'm no mathematician.
     
  6. X-Istence

    X-Istence * Political User

    Messages:
    6,498
    Location:
    USA
    seed it properly, if ona unix box, pipe /dev/random into the seeder, that should be good enough, but there is no gaurantee. Best bet is two queries, or one, and see if that fails, and if it does, then fix it.