Jump to content

help with mt_rand and switch


Go to solution Solved by Barand,

Recommended Posts

Hello Everyone,

 

I am working on a little project, which is basically creating a lottery draft order for our fantasy football league (I know uber cool right? lol).

 

I'm basically trying to mimic the old NBA draft lottery by doing something like the following:

 

Say our league has 12 teams and I want to weight each players chance of getting that 1st pick in the draft?  

 

Well I would give the worst player 12 chances out of 78, 2nd to last 11 chances out of 78 etc.

 

So I basically created an array with values 1-78 to be my overall chances and some other arrays to assign numbers to each team member i.e. 1-12 to last place, 13-23 for send to last etc.

 

and I was planning to do a while loop and array_diff to narrow down the chances.  here is what i have thus far:

while($x <= 12){

    $num = $adjusted[mt_rand(0,count($adjusted)-1)];
    
    Switch($num){
        
        case in_array($num,$place12):
        
            echo $x + ' ' + 'Player12';
        
            $adjusted = array_diff($adjusted,$place12);
            
            $x++;
        break;
        
        case in_array($num,$place11):
        
            echo $x + ' ' + 'Player11';
        
            $adjusted = array_diff($adjusted,$place11);
        
            $x++;
        break;
        
        case in_array($num,$place10):
        
            echo $x + ' ' + 'Player10';
        
            $adjusted = array_diff($adjusted,$place10);
        
            $x++;
        break;
        
        case in_array($num,$place9):
        
            echo $x + ' ' + 'Player9';
        
            $adjusted = array_diff($adjusted,$place9);
        
            $x++;
        break;
        
        case in_array($num,$place8):
        
            echo $x + ' ' + 'Player8';
        
            $adjusted = array_diff($adjusted,$place8);
        
            $x++;
        break;
        
        case in_array($num,$place7):
        
            echo $x + ' ' + 'Player7';
        
            $adjusted = array_diff($adjusted,$place7);
        
            $x++;
        break;
        
        case in_array($num,$place6):
        
            echo $x + ' ' + 'Player6';
        
            $adjusted = array_diff($adjusted,$place6);
        
            $x++;
        break;
        
        case in_array($num,$place5):
        
            echo $x + ' ' + 'Player5';
        
            $adjusted = array_diff($adjusted,$place5);
        
            $x++;
        break;
        
        case in_array($num,$place4):
        
            echo $x + ' ' + 'Player4';
        
            $adjusted = array_diff($adjusted,$place4);
        
            $x++;
        break;
        
        case in_array($num,$place3):
        
            echo $x + ' ' + 'Player3';
        
            $adjusted = array_diff($adjusted,$place3);
        
            $x++;
        break;
        
        case in_array($num,$place2):
        
            echo $x + ' ' + 'Player2';
        
            $adjusted = array_diff($adjusted,$place2);
        
            $x++;
        break;
        
        case in_array($num,$place1):
        
            echo $x + ' ' + 'Player1';
        
            $adjusted = array_diff($adjusted,$place1);
        
            $x++;
        break;
        
        default:
        
            $adjusted = $lottery;

        break;
}               
}

Just starting out on the project so it's still pretty basic.  Just trying to work on functionality right now.  I am getting an undefined offset error which i assume is related to the way i'm doing and storing the mt_rand value.  

 

any thoughts on how i might fix it?

Link to comment
https://forums.phpfreaks.com/topic/292485-help-with-mt_rand-and-switch/
Share on other sites

When you use switch($num) the case values should be possible values of $num

 

eg

switch ($dayOfWeek) {
  case 6:
    echo "Saturday";
    break;
  case 0:
    echo "Sunday";
    break;
  default:
    echo "Weekday";
    break;
}

Alternatively, as in_array() returns true or false, you could

switch (true) {
  case $dow == 6:
    echo "Saturday";
    break;
  case $dow == 0:
    echo "Sunday";
    break;
  default:
    echo "Weekday";
    break;
}

Also, the string concatenation operator in PHP is a period (.) and not '+'

Thanks Sen that is helpful.  I'm getting close to what I'm looking for however it doesn't appear that the while loop is working quite the way i was expecting.  

 

if you see i was hoping to remove the players chance from the next iteration of the loop and select a new random number.  I see that it keeps selecting numbers that are not in the array, which leads me to believe that it is not using the newly assigned array value correctly.  

 

any thoughts?

Edited by floorfiller

lol sorry about that Barand.

 

so for instance let's say these are the two arrays:

$adjusted = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30
,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78);

$place12 = array(1,2,3,4,5,6,7,8,9,10,11,12);

I would just want to subtract them with the array_diff function and rerun the while loop again using that new array value.  

  • Solution

Does this give what you want?

<?php
// players in order of best to worst
$players = array (
        1 => 'Player_1',
             'Player_2',
             'Player_3',
             'Player_4',
             'Player_5',
             'Player_6',
             'Player_7',
             'Player_8',
             'Player_9',
             'Player10',
             'Player11',
             'Player12',
        );
// put player12 into an array 12 times, player11 11 times etc
$adjusted = array();
foreach ($players as $k=>$p) {
    $tmp = array_fill(0,$k,$p);
    $adjusted = array_merge($adjusted, $tmp);
}
shuffle($adjusted);
$adj = array_unique($adjusted);
echo '<pre>',print_r($adj, true),'</pre>';

?>
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.