Flip a Coin in PHP
First we get the number of seconds in the current time on the server. This server's time was 9:16:27 pm when you opened this page (the 27 is the number of seconds).
$now_seconds = date("s");
All we do next is check to see if the number is even or odd.
if(($now_seconds - (2 * floor($now_seconds/2))) == 0)
{
// the number is even
}
else
{
// the number is odd
}
That's it. Here's a demonstration example:
$now_seconds = date("s");
if(($now_seconds - (2 * floor($now_seconds/2))) == 0)
{
echo $now_seconds . " is an even number";
}
else
{
echo $now_seconds . " is an odd number";
}
Here's the result:
Reload the page to see the result change.
Flip a 3-sided coin
If you want to make a choice between 3 options, you need a three-sided coin. Here's a working example script of how to do it in PHP:
$now_seconds = date("s");
if($now_seconds == 0) { $now_seconds = 60; }
if(($now_seconds - (3 * floor($now_seconds/3))) == 0)
{
echo $now_seconds . " is divisible by 3";
}
elseif(($now_seconds - (2 * floor($now_seconds/2))) == 0)
{
echo $now_seconds . " is an even number (and not divisible by 3)";
}
else
{
echo $now_seconds . " is an odd number (and not divisible by 3)";
}
Here's the result:
Reload the page to see the result change.
Here are the complete sets of numbers:
- Column 3 holds the 20 numbers between 1 and 60 that are divisible by 3.
- Column 2 holds the remaining 20 numbers that are even and not divisible by 3.
- And column 1 holds the remaining 20 odd numbers not divisible by 3.
3 | 2 | 1 |
- 3
- 6
- 9
- 12
- 15
- 18
- 21
- 24
- 27
- 30
- 33
- 36
- 39
- 42
- 45
- 48
- 51
- 54
- 57
- 60
|
- 2
- 4
- 8
- 10
- 14
- 16
- 20
- 22
- 26
- 28
- 32
- 34
- 38
- 40
- 44
- 46
- 50
- 52
- 56
- 58
|
- 1
- 5
- 7
- 11
- 13
- 17
- 19
- 23
- 25
- 29
- 31
- 35
- 37
- 41
- 43
- 47
- 49
- 53
- 55
- 59
|