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.

Multiple Randoms On Same Page

Postby Fireal » Sat Nov 25, 2017 2:54 pm

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
Fireal
 
Posts: 11
Joined: Sun May 21, 2017 12:51 pm
Has thanked: 5 times
Been thanked: 0 time

Re: Multiple Randoms On Same Page

Postby Jackolantern » Sat Nov 25, 2017 4:10 pm

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
User avatar
Jackolantern
 
Posts: 10893
Joined: Wed Jul 01, 2009 6:00 pm
Location: Houston, TX
Has thanked: 22 times
Been thanked: 92 times
Blog: View Blog (1)

Re: Multiple Randoms On Same Page

Postby Fireal » Sat Nov 25, 2017 4:56 pm

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
Fireal
 
Posts: 11
Joined: Sun May 21, 2017 12:51 pm
Has thanked: 5 times
Been thanked: 0 time

Re: Multiple Randoms On Same Page

Postby a_bertrand » Sun Nov 26, 2017 12:00 am

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
User avatar
a_bertrand
New Worlds Engine creator
 
Posts: 1535
Joined: Mon Feb 25, 2013 8:46 am
Has thanked: 15 times
Been thanked: 23 times

Re: Multiple Randoms On Same Page

Postby Fireal » Sun Nov 26, 2017 3:35 pm

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!
User avatar
Fireal
 
Posts: 11
Joined: Sun May 21, 2017 12:51 pm
Has thanked: 5 times
Been thanked: 0 time


Return to Beginner Help and Support

Who is online

Users browsing this forum: No registered users and 1 guest

cron

x