floorfiller Posted November 15, 2014 Share Posted November 15, 2014 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? Quote Link to comment https://forums.phpfreaks.com/topic/292485-help-with-mt_rand-and-switch/ Share on other sites More sharing options...
Barand Posted November 15, 2014 Share Posted November 15, 2014 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 '+' Quote Link to comment https://forums.phpfreaks.com/topic/292485-help-with-mt_rand-and-switch/#findComment-1496669 Share on other sites More sharing options...
floorfiller Posted November 15, 2014 Author Share Posted November 15, 2014 (edited) 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 November 15, 2014 by floorfiller Quote Link to comment https://forums.phpfreaks.com/topic/292485-help-with-mt_rand-and-switch/#findComment-1496672 Share on other sites More sharing options...
Barand Posted November 15, 2014 Share Posted November 15, 2014 Not offhand, member. I don't know what your $adjusted array looks like. Quote Link to comment https://forums.phpfreaks.com/topic/292485-help-with-mt_rand-and-switch/#findComment-1496674 Share on other sites More sharing options...
floorfiller Posted November 15, 2014 Author Share Posted November 15, 2014 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. Quote Link to comment https://forums.phpfreaks.com/topic/292485-help-with-mt_rand-and-switch/#findComment-1496675 Share on other sites More sharing options...
Solution Barand Posted November 15, 2014 Solution Share Posted November 15, 2014 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>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/292485-help-with-mt_rand-and-switch/#findComment-1496689 Share on other sites More sharing options...
floorfiller Posted November 17, 2014 Author Share Posted November 17, 2014 thanks again Barand. I ended up doing something a little different, but your post definitely pointed me in the right direction. Quote Link to comment https://forums.phpfreaks.com/topic/292485-help-with-mt_rand-and-switch/#findComment-1496775 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.