Welcome Guest ( Log In | Register )



 
Reply to this topicStart new topic
> Random With Functions?
Feelay
post Mar 20 2008, 02:42 PM
Post #1


Kinda N00B
Group Icon

Group: Members
Posts: 230
Joined: 13-January 08
From: Sweden
Member No.: 27,579



Hey!

Is it possible to use a random script on functions.
Lets say I have created three functions (Items that a user will win if he defeates a monster).

One function is a function named shield() and another function is named sword() and a third function is named helmet().
Now when a player defeates a monster, he must be awarded. So.. How can I randomize what item he should get? something like
CODE
rand(sheild(), sword(), helmet());

or?

Thanks //Feelay
Go to the top of the page
 
+Quote Post
turbopowerdmaxst...
post Mar 21 2008, 08:21 AM
Post #2


Premium Member
Group Icon

Group: [HOSTED]
Posts: 392
Joined: 16-February 06
From: Kolkata, India
Member No.: 11,322



You can only use the rand function to generate a random number. However, this number can be used to call any of those functions randomly. This can be easily accomplished using a switch - case statement.

CODE
switch(rand(1, 3))
{
    case 1:
           sheild();
        break;

    case 2:
        sword();
        break;

    case 3:
        helmet();
        break;
}
Go to the top of the page
 
+Quote Post
Feelay
post Mar 21 2008, 08:51 AM
Post #3


Kinda N00B
Group Icon

Group: Members
Posts: 230
Joined: 13-January 08
From: Sweden
Member No.: 27,579



oh my tongue.gif Never wanted to learn how to use that switch case thing tongue.gif Well.. I think I will have to learn then.. Thank You ^-^

//Feelay
Go to the top of the page
 
+Quote Post
TavoxPeru
post Mar 21 2008, 09:23 PM
Post #4


Super Member
Group Icon

Group: [HOSTED]
Posts: 763
Joined: 8-April 06
From: Lima - Peru
Member No.: 12,579



I agree with turbopowerdmaxsteel, that's the way to do that in my opinion, but, another way to do the same thing is by using a series of IF and ELSEIF statements on the same expression, for example the following code is another way to write the same thing:

CODE
<?php
$i=rand(1, 3);
if ($i == 1) {
   sheild();
} elseif ($i == 2) {
   sword();
} elseif ($i == 3) {
   helmet();
}
?>

Don't forget to always include a break statement at the end of every case, because the switch statement executes line by line and if you don't include it PHP will continue executing the next code until it finds a break statement or the end of the switch block. Also, it is recommended to include a default case that will be executed in case anything match.

QUOTE
A special case is the default case. This case matches anything that wasn't matched by the other cases, and should be the last case statement.

For more information go to PHP: switch - Manual.

Best regards,
Go to the top of the page
 
+Quote Post
Jared
post May 8 2008, 10:20 AM
Post #5


Member [ Level 1 ]
Group Icon

Group: [HOSTED]
Posts: 40
Joined: 17-April 08
Member No.: 29,853



Yes you may find at some point through editing that the amount of rewards may change. So it is always wise to have as much error checking in your script as possible:

CODE
$rewards_num = 3;
$reward = rand (1, $rewards_num); // for now, there are 3 options.

switch ($reward) {
    case 1: helmet (); break;
    case 2: sword (); break;
    case 3: helmet (); break;
    default: wrong_reward (); break; // this would be a function containing a returned value from the rand () that is invalid
}


And as the previous poster mentioned, check out that link for more info on the switch.
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. String Library Functions(4)
  2. C++: Basic Classes(5)
  3. Php: Lesson #3;functions(0)
  4. Php Random Selector(2)
  5. Blingo Search Engine(3)
  6. Using Regular Expressions To Parse Functions(5)
  7. Random Images On Eacht Pageload And Refresh.(6)
  8. Coppermine Random Image Script(6)
  9. Visual Basic: Random Strings!(10)
  10. Php : Variables Included Dont Work In Functions(4)
  11. Creating Dll With Delphi(0)
  12. C# Tutorial : Lesson 8 - Functions/methods(0)
  13. Javascript Show / Hide Functions(3)
  14. Writing Functions In PHP(6)
  15. How To Get The Random.h Header(4)
  1. Random Name Generator(2)
  2. Google Image Labeler(11)
  3. Script For Viewing A Random Image Needed(3)
  4. Rather Random Question(2)
  5. Lesson #3 - Basic Math Operators And Random #s(0)
  6. Calling Of Functions Between Mulitple External Javascript Files(2)
  7. Google Adsense's Functions(1)
  8. A Few Random Thoughts..(0)
  9. PHP And Texts: Need Help With PHP String Functions(5)
  10. Multibyte-string Functions(1)
  11. Php: Write Random Text As Image(3)
  12. Random MySQL Entry(1)
  13. Random Chuck Norris Facts(2)
  14. Pre-installed Cgi(0)
  15. Photoshop Tutorial: Abstract Background I(1)


 



- Lo-Fi Version Time is now: 12th October 2008 - 03:40 PM