SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot
    Join Date
    Jul 2002
    Location
    USA
    Posts
    132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Secret Santa code

    I'm doing a program for my forum members that allows them to sign up to be a "Secret Santa" and be a gift giver to someone anonymously.

    Wondering the cleverish way to do the random assignment of who gives to who.

    So there are 100 people (say)..
    person 1 gets assigned a random person from the 99 remaining.
    person 2 gets assigned a random person from the 98 remaining.
    ..
    person 100 gets assigned the last person remaining.

  2. #2
    SitePoint Wizard Dylan B's Avatar
    Join Date
    Jul 2004
    Location
    NYC
    Posts
    1,150
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    mysql query("SELECT * FROM users WHERE santa='none' ORDER BY rand()");

    Or something.

  3. #3
    SitePoint Guru
    Join Date
    Jul 2004
    Location
    Raleigh, NC
    Posts
    783
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you can put all id.s into an array and shuffle() the array. after you do that, use a foreach loop to see if any key==value. if no key==value, you're done. if more than 1 key== value, treat these as a new array and move each value up one space. ex:
    array [3] => 3, [18] => 18, [40] => 40
    becomes
    array [3] => 18, [18] => 40, [40] => 3

    if there's only 1 key==value, switch values with the place beneath it. ex:
    array ... , [4] => 15, [5] => 5 , [6] => 16 , ...
    becomes
    array ... , [4] => 15, [5] => 16 , [6] => 5 , ...

  4. #4
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    if(!function_exists("array_combine")) {
        
        
    // this is for php4
        // in php5 array_combine is a builtin function
        
        
    function array_combine($keys$values) {
            
    $result = array();
            for(
    $i 0$c count($keys); $i $c$i++)
                
    $result[$keys[$i]] = $values[$i];
            return 
    $result;
        }
    }

    // this creates random 'pairs' list from an array

    function random_pairs($arr) {
        
    shuffle($arr);
        return 
    array_combine(
            
    array_splice($arr0count($arr) >> 1), 
            
    $arr);
    }

    // test

    echo "<pre>";
    $a = array(1,2,3,4,5,6,7,8,9,10);
    $pairs random_pairs($a);
    print(
    "Random pairs are\n");
    print_r($pairs); 
    hth...

  5. #5
    SitePoint Guru
    Join Date
    Jul 2004
    Location
    Raleigh, NC
    Posts
    783
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by stereofrog
    PHP Code:
    if(!function_exists("array_combine")) {
        
        
    // this is for php4
        // in php5 array_combine is a builtin function
        
        
    function array_combine($keys$values) {
            
    $result = array();
            for(
    $i 0$c count($keys); $i $c$i++)
                
    $result[$keys[$i]] = $values[$i];
            return 
    $result;
        }
    }

    // this creates random 'pairs' list from an array

    function random_pairs($arr) {
        
    shuffle($arr);
        return 
    array_combine(
            
    array_splice($arr0count($arr) >> 1), 
            
    $arr);
    }

    // test

    echo "<pre>";
    $a = array(1,2,3,4,5,6,7,8,9,10);
    $pairs random_pairs($a);
    print(
    "Random pairs are\n");
    print_r($pairs); 
    hth...
    doesn't work with an odd number


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
  •