Jump to content

problem with mt_rand


cloudll

Recommended Posts

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();
    }
Link to comment
https://forums.phpfreaks.com/topic/296169-problem-with-mt_rand/
Share on other sites

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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.