SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    monitormensch oerdec's Avatar
    Join Date
    Sep 2004
    Location
    Hamburg
    Posts
    706
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    random function for Advent calendar

    Hello all,

    I am working on an online advent calendar where users can win prizes.
    And I donīt have a clue how to realize it.

    Registered users can open a window every day until the 24th of December.
    There are two possibilites of what they get.
    Either they can see a funny picture or they get a symbol.
    These symbols were collected.
    With the right combination of three symbols they win a prize.

    There are 4 categories of prizes. Just examples:

    First category: 1 X a nice bungalow
    Second category: 10 X a Porsche 911
    Third category: 333 X a Digicam
    Forth category: 5555 X an ugly pen

    My problem is the random function for this. There is just ONE bungalow.

    How to write a script that doesnīt deliver 776 bungalows?

    Additional notes:

    I created two tables in a database.
    The first one stores the prizes and the amount of it.
    The second is for users and what they opened every day.

    I get a serious headache from this job

    oerdec//

  2. #2
    SitePoint Guru
    Join Date
    Jul 2004
    Location
    Raleigh, NC
    Posts
    783
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you can make a copy of your list for what remains. this would be the list you select from. you can also subtract items from that list as they are used up

  3. #3
    SitePoint Wizard Chris82's Avatar
    Join Date
    Mar 2002
    Location
    Osnabrück
    Posts
    1,003
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    I can win a Porsche 911? Let me code this for you

    Code:
    UPDATE users SET prize =  'Porsche' WHERE name = 'Myself';
    Joking aside. My thoughts:

    • a total of 5899 prizes: either 5899 combinations (or only 4 one for each category)
    • you have 3 symbols which gives you 3! = 6 possibilities to encode a winning combination.
    • you probably need to calculate the combinations for each user in advance. (maybe use a fixed deadline for

      registration: sign up before 30.11 and take part in our "gewinnspiel")
    • I have attempted to generate a random combination generator but it has some problems still.
    • If you have four prices it is maybe better to encode the prizes with four symbols instead of three.


    PHP Code:
    <pre>
    <?php

    set_time_limit
    (0);

    mysql_connect('localhost''root') or die('error connecting');
    mysql_select_db('test') or die('error db');

    mysql_connect('localhost''root') or die('error connecting');
    mysql_select_db('test') or die('error db');

    mysql_query('DROP TABLE IF EXISTS testcomb') or die('error dropping');
    mysql_query('CREATE TABLE testcomb (combination CHAR(3))') or die('error dropping');


    mysql_query('DELETE from testcomb') or die('error deleting ' mysql_error());

    $symbol_mapping = array('s''p''t');

    // alphabetically sorted keys are important 
    // each combination of letters encodes a prize category. 
    $c = array(
        
    'sss' => 1
        
    'sst' => 10
        
    'pps' => 333,
        
    'pst' => 5555,
    );

    // problem is this not a good random distribution
    // we would need two random number generators
    mt_srand();
    $runs 0;
    $n 1// mysql_num_rows('users');
    for ($i 1$i 6000$i++) {
        
    $test '';
        
    $last '';
        
    $seq '';
        while (
    strlen($seq) < 24) {
            
    $val mt_rand(0,1);
            
    // 3 symbols and 24 days
            
    if ($val 3/24) {
                
    // generate random symbol
                
    $symbol mt_rand() % 3;
                
    // store current string;
                
    $seq .= $symbol_mapping[$symbol];
                
    $last $seq;
                
    $test .= $symbol_mapping[$symbol];
                
                
    # prepare test string
                
    $test str_replace('*'''$test);
                
    $test preg_split('//'$test, -1PREG_SPLIT_NO_EMPTY);
                
    sort($test);
                
    $test implode(''$test);
                
                
    # check if the string $test has already 3 letters
                
    if (strlen($test) == 3) {
                    
    # check for each combination if there are still prizes left
                    
    if (in_array($testarray_keys($c)) && $c[$test] == 0) {
                        
    $seq substr($last0, -1);
                        
    $test substr(str_replace('*'''$test), 0, -1);
                        continue;
                    }
                    else if (
    in_array($testarray_keys($c)) && $c[$test] > 0) {
                        
    $c[$test]--;
                        
    #mysql_query("INSERT INTO testcomb VALUES ('$test')");
                        
    break;
                    }
                    else {
                        
    #mysql_query("INSERT INTO testcomb VALUES ('$test')");
                        
    break;
                    }
                }
            }
            else {
                
    $seq .= '*';
            }
        }
        
    mysql_query("INSERT INTO testcomb VALUES ('$test')");
        
    printf("i: %4d -> %24s\n"$i$seq);
    }
    ?>
    What is does is this:

    It has an encoded combination for each prize category. In the while-loop it generates a random number between 0 and 1. If this number is less than 3/24 = 0.125 a new number is generated. (I chose 3/24 since you have three symbols and 24 days until christmas.) The newly generated number is inside the interval [0;3[ and is then used to map it to a symbol. ($symbol_mapping[$symbol]).
    Since a new symbol was added, the string needs to be tested. The if-conditions check if this is a winning combination in which case it decreases the counter for the corresponding category. A check is also included if the total number of prizes in the category has already been exceeded in which case the last added character is removed.
    I think this works but I'm not totally sure. It has problems with the randomness as it uses the same seed for both the random number for either symbol or pic and for the actual symbol.

    If you do a

    Code:
    SELECT combination, count( combination )
    FROM `testcomb`
    GROUP BY combination
    On the testcomb table you will see that the prizes are added only as often as they are available.

  4. #4
    monitormensch oerdec's Avatar
    Join Date
    Sep 2004
    Location
    Hamburg
    Posts
    706
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Chris,

    thanks a lot for your help. Really cool!

    But there is a problem. I donīt have just three different symbols but 12.
    Itīs not like a one-armed bandit this gambling machine.

    I donīt know if itīs clever just to alter the specific parts, like

    PHP Code:
    $symbol_mapping = array('s''p''t'); 
    and so on.

    Then is the next question: Are users able to win the bungalow or the Porsche?
    I mean maybe is the probability too little.

    oerdec//

  5. #5
    SitePoint Wizard Chris82's Avatar
    Join Date
    Mar 2002
    Location
    Osnabrück
    Posts
    1,003
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi oerdec.

    You use 12 symbols and the winning combination will just be 3 symbols for each category? I'm a bit lost on the probability and possibilities now.

    The code I posted does not ensure that every prize is distributed. The bungalow and porsches were always gone but the cameras and pens were not used up completely. I ran the test only 6000 times. So maybe for a greater number all prizes will be used up.

    The problem with this apporach is that you cannot assure that each prize is distributed. A certain combination might never occur. The way I see it, it is like Lotto, albeit with fewer possible combinations. The jackpot is not "cracked" every week, either.

    The easier approach is probably to randomly pick the users for each category. This way you can assure that each prize is taken.


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
  •