Jump to content

problem with mt_rand


cloudll
Go to solution Solved by mac_gyver,

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
Share on other sites

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?

Link to comment
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.

Link to comment
Share on other sites

  • Solution

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 by mac_gyver
  • Like 1
Link to comment
Share on other sites

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.