cloudll Posted May 9, 2015 Share Posted May 9, 2015 Hey guys, I'm having some trouble with mt_rand on a game I'm working on, at least I think thats the problem. If the activeBattle condition is 'no', and the number 1 is generated by mt_rand, the code is supposed to start a battle by updating my database. The only issue is, it seems to be starting the battle on any number. Its completly random which number activates the battle. There is no pattern. Here is the code, does anyone have any ideas why this may be happening? $encounter = mt_rand(1, 6); if (($encounter == 1) && ($activeBattle == 'no')) { $active = "yes"; $id = 1; $sql = "UPDATE encounter SET active=? WHERE id=?"; $statement = $connect->prepare($sql); $statement->execute(array($active,$id)); reloadPage(); } Quote Link to comment https://forums.phpfreaks.com/topic/296169-problem-with-mt_rand/ Share on other sites More sharing options...
mac_gyver Posted May 9, 2015 Share Posted May 9, 2015 what are the actual $encounter values when you log/echo them right before the if(){} conditional statement and are you sure that's the actual code that's being ran on the server? Quote Link to comment https://forums.phpfreaks.com/topic/296169-problem-with-mt_rand/#findComment-1511258 Share on other sites More sharing options...
ginerjm Posted May 9, 2015 Share Posted May 9, 2015 How about doing some echo-ing of your random number to be sure that is what is happening. Plus - what happens in reloadpage()? Do you reset activeBattle from the value of active in your table that you just updated? Quote Link to comment https://forums.phpfreaks.com/topic/296169-problem-with-mt_rand/#findComment-1511259 Share on other sites More sharing options...
cloudll Posted May 9, 2015 Author Share Posted May 9, 2015 I have been echo'ing the random number and it is correctly outputting a number from 1-6. But the battle is still activating on any number it feels like. But with no pattern what so ever. Sometimes it's a 1, sometimes a 5 etc. The reload function just updates the script to reflect the changes but I actually removed it to test this error and just forgot to take it out of this post. Quote Link to comment https://forums.phpfreaks.com/topic/296169-problem-with-mt_rand/#findComment-1511261 Share on other sites More sharing options...
ginerjm Posted May 9, 2015 Share Posted May 9, 2015 There's nothing wrong with the code that you are showing us. What code is causing the 'battle' to start up again and again? That is where the fault lies. Quote Link to comment https://forums.phpfreaks.com/topic/296169-problem-with-mt_rand/#findComment-1511268 Share on other sites More sharing options...
jcbones Posted May 9, 2015 Share Posted May 9, 2015 Perhaps it isn't in the battle start, but in the battle end. As in "how do you end the battle?" Quote Link to comment https://forums.phpfreaks.com/topic/296169-problem-with-mt_rand/#findComment-1511269 Share on other sites More sharing options...
Solution mac_gyver Posted May 10, 2015 Solution Share Posted May 10, 2015 (edited) i'm going to guess that you have a redirect-loop or similar that's requesting the page multiple times. the symptom you are seeing of random numbers other than a 1 causing the query to update is just the last random value being echoed, but in fact the code has ran several times, one of which had a 1 that caused the query to run. you may also have some other code that's always running the same query or have a logic error that's updating that value when it should be resetting it. at this point, you have a page that doesn't do what you expect. it will likely take seeing all the code on the page that reproduces the problem (less any database credentials) in order to help you. Edited May 10, 2015 by mac_gyver 1 Quote Link to comment https://forums.phpfreaks.com/topic/296169-problem-with-mt_rand/#findComment-1511285 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.