PDA

View Full Version : PHP chance


MinatureCookie
May 27th, 2007, 10:36
:hi: Hey guys.
I'm looking for a PHP script that chooses something out of random, but has a changeable 'chance'. For example, if the POST data was 5, then (1 / 5 = 0.2) the chance of one thing happening is 0.2, the chance of the other happening is 0.8
I'm Googling this, but not much useful stuff is coming up, things like give 'Give PHP a chance' :bored:

Lol, so if any of you have done something like this before, cheers ;)

Jamie
May 27th, 2007, 11:00
From somebody who has a great lack of PHP knowledge, I would say the way to do this would be to have the script call the values from a .txt file, and then simply write the one you want to have a higher chance more times than the lower chance value.

So if you wanted the value "green" to be three times as likely to be chosen than "red", your .txt file might look like this:

Red
Red
Green
Green
Green
Green
Green
Green

I guess that would be the dummy and common sense way to do it. In fact, the PHP script we use to rotate the sponsors up in the top left corner of IWDN uses this technique. Obviously we give each sponsor the same "chance", but if we wanted, we could give another sponsor a higher chance simply by writing the sponsor text several more times in the file that the script calls the values from. :)

inimino
May 27th, 2007, 12:20
1. Get a random number.
2. Check if it is evenly divisible by 5.

There you go. ;)

MinatureCookie
May 27th, 2007, 12:47
I don't think the ones you suggested quite fit the POST data thing, 'cos it will keep changing. Inimino's might have, I'm not sure :P

Anyway, I found a good one that works for me:
http://snippets.dzone.com/posts/show/2451
I'm not quite honestly sure how it works, but when it goes into decimals it inverts the chances (so the smaller the decimal the higher chance).
Thanks =]