Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    Regular Coder Armondo's Avatar
    Join Date
    Feb 2007
    Posts
    144
    Thanks
    3
    Thanked 0 Times in 0 Posts

    generate completely random number

    i am trying to generate numbers that are so random they are not likely to be repeated ever....i currently have this code:
    PHP Code:
    $topicID mt_rand(0,100);
    $topicID .= mt_rand(50,10000);
    $topicID .= mt_rand(75,1000000);
    $topicID .= mt_rand(100,1000000000000000); 
    i just thought that that would create a unique number, but seeing that it is a random number...i think that there might be a chance that this $topicID number will be repeated. is there a better way to do this?
    ..
    ▲ ▲

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    If you use a timestamp value out to the millisecond that will never be repeated...

    If you're using this value as a unique ID in a MySQL table why not just use an auto-increment integer column?

  • #3
    Regular Coder
    Join Date
    May 2007
    Posts
    130
    Thanks
    9
    Thanked 0 Times in 0 Posts
    i would go with Fumigator's auto-increment suggestion. the id will never be repeated and automatically increase to the next value.

    but if you're certain you need a random number generator let me know... i could help with a function where you say how long you want your number/string and it will return a random alpha-numeric code of your desired length.

  • #4
    Regular Coder Armondo's Avatar
    Join Date
    Feb 2007
    Posts
    144
    Thanks
    3
    Thanked 0 Times in 0 Posts
    well, i thought of that...but you can only have one auto increment thing per table...unless its a key. is there a way to do that? i mean, how would i make it to where it does that?

    i dont think i can do that though, if it is auto incrementing then each entry gets a new number...i just want the incremented number to be assigned to other posts...but...umm...i really dont know.
    ..
    ▲ ▲

  • #5
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    You already have an auto-increment ID column in the table? Then why not just use that column?

    After inserting a row in a table with an auto-increment key you can grab that value using mysql_insert_id().

  • #6
    Regular Coder Armondo's Avatar
    Join Date
    Feb 2007
    Posts
    144
    Thanks
    3
    Thanked 0 Times in 0 Posts
    ooh, that makes sense...but how do i grab that value before i put it in the database and then duplicate it? like:
    PHP Code:
    $topicID $id
    ?
    ..
    ▲ ▲

  • #7
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    You can't... you can only grab it after the insert is done.
    PHP Code:
    $query "INSERT INTO topic_table blah blah blah";
    $result mysql_query($query);
    if (!
    $result) {
        die(
    "SQL Error in query: $query<br />error text: ".mysql_error());
    }
    $topicID mysql_insert_id(); 

  • #8
    New to the CF scene
    Join Date
    Jun 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi.

    Take a look at the uniqid() function

  • #9
    Regular Coder Armondo's Avatar
    Join Date
    Feb 2007
    Posts
    144
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by EloyQQQ View Post
    Hi.

    Take a look at the uniqid() function
    thanks alot. i know this is not the best way to accomplish this, but it works. maybe in later versions of my script i will change it...but this is great. i changed the code for the uniqid() to this:
    PHP Code:
    $topicID md5(uniqid(rand(), true)); 
    i seriously doubt that will be repeated.
    output:
    Code:
    d41d8cd98f00b204e9800998ecf8427e
    my script is here: http://flashanims.com/forum/index.php

    i am trying to code a forum from scratch instead of using prebaked forum software
    Last edited by Armondo; 06-29-2007 at 07:43 PM.
    ..
    ▲ ▲

  • #10
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    Quote Originally Posted by Armondo View Post
    ooh, that makes sense...but how do i grab that value before i put it in the database and then duplicate it? like:
    PHP Code:
    $topicID $id
    ?
    You want to duplicate a column? Instead of trying to figure out how to do that (a trigger would be possible), why not solve the problem instead of solving the sympton?

    If your topic ID is the same as your row ID, then perhaps you should just use the row ID as the topic ID...

    There is no reason to generate some fancy number that you only 99% for sure will repeat. An auto increment will never repeat as long as there is no bug in the DBMS.

    So 100% or 99%, which is cleaner? Not to mention the auto increment is less code basically.

  • #11
    Regular Coder Armondo's Avatar
    Join Date
    Feb 2007
    Posts
    144
    Thanks
    3
    Thanked 0 Times in 0 Posts
    true, but then i would need to make other posts the same row id...and that screws everything up. i think that 99% is good, i seriously doubt that it will repeat. thanks anyway
    ..
    ▲ ▲


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •