Multiple Randoms On Same Page

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
Fireal
Posts: 11
Joined: Sun May 21, 2017 5:51 pm

Multiple Randoms On Same Page

Post 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!
User avatar
Jackolantern
Posts: 10893
Joined: Wed Jul 01, 2009 11:00 pm

Re: Multiple Randoms On Same Page

Post 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.
The indelible lord of tl;dr
Fireal
Posts: 11
Joined: Sun May 21, 2017 5:51 pm

Re: Multiple Randoms On Same Page

Post 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.
User avatar
a_bertrand
Posts: 1537
Joined: Mon Feb 25, 2013 1:46 pm

Re: Multiple Randoms On Same Page

Post 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?
Creator of Dot World Maker
Mad programmer and annoying composer
Fireal
Posts: 11
Joined: Sun May 21, 2017 5:51 pm

Re: Multiple Randoms On Same Page

Post 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!
Post Reply

Return to “Beginner Help and Support”