Results 1 to 5 of 5

Thread: how to generate random non repeated numbers in php

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2005
    Posts
    1,907

    Arrow how to generate random non repeated numbers in php

    Hi all. could an expert show me how i can generate random non repeated numbers. I want to use those number as file name that i write to. currently the file name is ram.txt but i want it the file name to be like 948450913.txt each time the scripts runs diffrent number .I be happy if an expert show me how to do that here in this code?thanks

    Code:
    <?php
    
    
    $url[1] = "http://localhost/flash_mp3_player/mp3/08 - Track 8.mp3";
    $url[2] = "http://localhost/flash_mp3_player/mp3/13 - Gar Aya.mp3";
    $url[3] = "http://localhost/flash_mp3_player/mp3/Soroush - Yeh Donya - 02 Shoghe Nafas.mp3";
    
    $mycontent = "";
    if (isset($_GET["sid"]))
    {
       $allsid = explode (",",$_GET["sid"]);
       $mycontent = array();
       foreach ($allsid AS $value)
          $mycontent[] = $url[$value];
    }
    
    echo $mycontent;
    
    $handle = fopen ("ram.txt","w+");
    if ($handle)
    {
      if (fwrite ( $handle,implode("\r\n",$mycontent)."\r\n") )
      {
          echo "FILE IS WRITTEN SUCCESSFULLY";
      } else
      {
           echo "ERROR IN WRITING TO FILE";
      }
      fclose ($handle);
    } else
    {
          echo "ERROR IN OPENING FILE";
    }
    require 'config.txt';
    ?>

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: how to generate random non repeated numbers in php

    Difficult, very difficult to do perfectly. (Or memory-intensive.)

    Realistically, though, if you just use uniqid() to generate an ID, the collision probability is so low that you can just form the filename, check if it exists, and if it does, request a new ID. Repeat until you find a free slot. It really shouldn't take more than two tries, three at the very most, and only if you're particularly unlucky and have a LOT of files already stored.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3
    <?="Moderator"?> john tindell's Avatar
    Join Date
    Jan 2002
    Location
    Brighton, UK
    Posts
    1,099

    Re: how to generate random non repeated numbers in php

    You could use the time that it was generated on by using time() (http://uk2.php.net/manual/en/function.time.php)

    PHP Code:
    $filename time() . ".txt";
    $handle fopen ($filename,"w+"); 

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: how to generate random non repeated numbers in php

    This might prove to have too little resolution: two requests within a second would attempt to open the same file.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2005
    Posts
    1,907

    Re: how to generate random non repeated numbers in php

    Quote Originally Posted by john tindell
    You could use the time that it was generated on by using time() (http://uk2.php.net/manual/en/function.time.php)

    PHP Code:
    $filename time() . ".txt";
    $handle fopen ($filename,"w+"); 
    many thanks for u reply. the whole idea of doing this is to generate unique file for each request from user .Do think by using this method no 2 user get same .txt file when they execute this page?Thanks
    Last edited by tony007; Feb 6th, 2006 at 02:17 PM.

Posting Permissions

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



Click Here to Expand Forum to Full Width