Looking for a way to make odds

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
xwhitchx
Forum Commoner
Posts: 38
Joined: Mon Jun 21, 2010 4:30 pm

Looking for a way to make odds

Post by xwhitchx »

Im looking to for a way to make something show up on a %, i need this to work with (if).

example, when the page is loaded text will say Good Day 95% of the time, and 4% says Hello, and 1% says You Win.


Sorry if I'm not making any sense, I'm finding this hard to put in to words.
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: Looking for a way to make odds

Post by twinedev »

Code: Select all

$intHit = rand(1,100);
if ($intHit <= 95) { 
  $strMsg = 'Good Day'; 
}
elseif ($intHit <= 99) { // 95 + 4, Anything 95 and under would have hit above 
  $strMsg = 'Hello'; 
}
else { // The only thing that would hit here is 100, which is 1% chance
  $strMsg = 'You Win'; 
}
xwhitchx
Forum Commoner
Posts: 38
Joined: Mon Jun 21, 2010 4:30 pm

Re: Looking for a way to make odds

Post by xwhitchx »

thank you very much. this should help me a lot
Post Reply