SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Addict SitePointer's Avatar
    Join Date
    Oct 2001
    Location
    SitePoint
    Posts
    317
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Generate 6 unique random numbers with PHP?

    How to generate a set of 6 random numbers
    - range from 1 to 100
    - each number must be unique
    - no number to be repeated.
    ?


  2. #2
    o_O O_o BlueFire2k5's Avatar
    Join Date
    Mar 2003
    Location
    Sioux Falls, SD
    Posts
    475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $randarray = array();
    for(
    $i 1$i <= 6; )
    {
        unset(
    $rand);
        
    $rand rand(1100);
        if(!
    in_array($rand$randarray))
        {
            
    $randarray[] = $rand;
            
    $i++;
        }
    }
    print_r($randarray); 
    Cheers.

  3. #3
    SitePoint Addict SitePointer's Avatar
    Join Date
    Oct 2001
    Location
    SitePoint
    Posts
    317
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks a lot.

    Your script print out the results in this format

    Array ( [0] => 37 [1] => 42 [2] => 43 [3] => 36 [4] => 41 [5] => 26 )

    May I know how can i remove the words: Array, [0] =>, [1] => .....

    and just print out the numbers only?

  4. #4
    Node mutilating coot timnz's Avatar
    Join Date
    Feb 2001
    Location
    New Zealand
    Posts
    516
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    for($i 1$i <= 6; )
    {
        unset(
    $rand);
        
    $rand rand(1100);
        if(!
    in_array($rand$randarray))
        {
            echo 
    $rand' ';
            
    $i++;
        }

    Gives on trial: 83 6 6 82 45 46 (er... random eh... and two 6's, hehe).
    Might I suggest you look up the print_r() function see what it does also.
    Oh no! the coots are eating my nodes!

  5. #5
    o_O O_o BlueFire2k5's Avatar
    Join Date
    Mar 2003
    Location
    Sioux Falls, SD
    Posts
    475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $randarray = array();
    for(
    $i 1$i <= 6; )
    {
        unset(
    $rand);
        
    $rand rand(1100);
        if(!
    in_array($rand$randarray))
        {
            
    $randarray[] = $rand;
            
    $i++;
        }

    $randstr implode(", "$randarray);
    echo 
    $randstr

  6. #6
    Node mutilating coot timnz's Avatar
    Join Date
    Feb 2001
    Location
    New Zealand
    Posts
    516
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    [nit-picking]Why bother storing it in the $randarray in the first place? That's just more work than is necessary to achieve the desired result.[/nit-picking]
    Oh no! the coots are eating my nodes!

  7. #7
    o_O O_o BlueFire2k5's Avatar
    Join Date
    Mar 2003
    Location
    Sioux Falls, SD
    Posts
    475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by timnz
    [nit-picking]Why bother storing it in the $randarray in the first place? That's just more work than is necessary to achieve the desired result.[/nit-picking]
    Not true. Notice the in_array so that you don't get two of the same numbers? You need an array for that.

  8. #8
    Node mutilating coot timnz's Avatar
    Join Date
    Feb 2001
    Location
    New Zealand
    Posts
    516
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oops, didn't see that.

    *actually reads thread from beginning and sees requirements - oh*

    Why the hell did I post what I did? My brain must have been majorly messed up... it was last day of term.... I had had three assignments/projects due the day before. I think I'll use them as my excuses, and it was "late" at night.

    I feel stupid now.
    Last edited by timnz; Sep 19, 2003 at 18:13.
    Oh no! the coots are eating my nodes!

  9. #9
    SitePoint Addict SitePointer's Avatar
    Join Date
    Oct 2001
    Location
    SitePoint
    Posts
    317
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    BlueFire2k5 and timnz, I want to say thank you for the help. You guys are wonderful. 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
  •