SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict
    Join Date
    Apr 2002
    Posts
    395
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    non-repeating random numbers?

    I have this code that picks 4 numbers from 1 to 5
    PHP Code:
    $num1 = array("1""2""3""4""5");
     
    srand((double)microtime()*1000000);
     
    $randval1 rand (0,(count($num1)-1));
     
     
    $num2 = array("1""2""3""4""5");
     
    srand((double)microtime()*1000000);
     
    $randval2 rand (0,(count($num2)-1));
     
     
    $num3 = array("1""2""3""4""5");
     
    srand((double)microtime()*1000000);
     
    $randval3 rand (0,(count($num3)-1));
     
     
    $num4 = array("1""2""3""4""5");
     
    srand((double)microtime()*1000000);
     
    $randval4 rand (0,(count($num4)-1)); 
    I want each randomly picked number to be different, so
    $num1, $num2, $num3 , and $num4 would be always different.

    How can I do so?

  2. #2
    SitePoint Enthusiast
    Join Date
    Oct 2005
    Location
    ATLANTA GA
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just remove each number from the array as it's chosen.

  3. #3
    SitePoint Guru aamonkey's Avatar
    Join Date
    Sep 2004
    Location
    kansas
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    look at the first example under shuffle()

  4. #4
    SitePoint Addict
    Join Date
    Apr 2002
    Posts
    395
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks


Bookmarks

Posting Permissions

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