PHPBuilder - Random Choices: Flip a Coin



RSS Twitter
Snippets Math Functions

Random Choices: Flip a Coin

by: Bill Pellowe
|
March 12, 2004

Version: 1.0

Type: Sample Code (HOWTO)

Category: Math Functions

License: GNU General Public License

Description: You can make a quick random choice between two options by seeing if the current server time seconds is even or odd. See a working version at http://www.i-fubar.com/flip-a-coin-script.php The webpage also shows how to flip a 3-sided coin.



// get the current server time in seconds
$now_seconds = date("s");
// see if it's even or odd 
if(($now_seconds - (2 * floor($now_seconds/2))) == 0) 
{ 
  // the number is even 
}
else 
{ 
  // the number is odd 
} 

/* that's it! You've flipped a coin.
   See a working version here:
   http://www.i-fubar.com/flip-a-coin-script.php
   Includes a way to flip a 3-sided coin, too.
*/

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
Bill Pellowe

Comment:



Comment:

(Maximum characters: 1200). You have characters left.