Page 1 of 1

Multiple Randoms On Same Page

Posted: Sat Nov 25, 2017 7:54 pm
by Fireal
Hello, I'm still working with getting all my randoms set up for a fighting game I'm working on. My issue is using the same random function twice, once per fighter, to determine a winner. Except, I keep getting the

Code: Select all

Fatal error: Cannot redeclare random_int()
issue. My question is how do you go about getting multiple random numbers in the same page with out issue? Thanks!

Re: Multiple Randoms On Same Page

Posted: Sat Nov 25, 2017 9:10 pm
by Jackolantern
It isn't actually a problem with creating a random number. The issue is that your code is redeclaring the random_int() function. Likely a file is being included multiple times that declares the random_int() function multiple times. You should look at the code that declares that function and then trace back how it is being included into the application.

Re: Multiple Randoms On Same Page

Posted: Sat Nov 25, 2017 9:56 pm
by Fireal
Thanks Jack. However, I still can't seem to figure it out.

I declare the function with

Code: Select all

function random_int ($min, $max) {return (mt_rand($min,$max));}
and call it with

Code: Select all

$matchFinish = random_int(1,10);
I have tried moving the declaration in the same loop as the call and outside the loop. It is only called once as well. I appreciate any kind of breakdown of this issue a bit more.

Re: Multiple Randoms On Same Page

Posted: Sun Nov 26, 2017 5:00 am
by a_bertrand
A function must declared only once. So for sure the function declaration must be outside of the loop, but I suspect it will still be called multiple times, can you show us the whole piece of code?

Re: Multiple Randoms On Same Page

Posted: Sun Nov 26, 2017 8:35 pm
by Fireal
Thanks a_bertrand, that was it! I was losing track of the function declaration in my spaghetti code. It was nested in a loop. Thanks guys!