Forum Moderators: coopster

Message Too Old, No Replies

Creating "Odds"

         

wbsmperry

2:12 pm on Aug 24, 2008 (gmt 0)

10+ Year Member



trying to create an odds system. Say, you have a 1 in 1000 chance of picking the right number. So if I click the link, it will show a number between 1 and 1000. but it should be number X at least once every 1000 times the page is loaded.... I hope that makes sense.

Im basically just wondering if anyone knows of a quick way of accomplishing this.

Thanks in advance!

henry0

3:17 pm on Aug 24, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'll write a counter
Next after 999
Override the counter and force a match

if you can not figure how writing a counter
you will find tons of such by G for php counter.

whoisgregg

4:52 pm on Aug 28, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Do you want it to be 1 in 1,000 per user? Then you'll need to store each users own personal list of numbers. This example shows using sessions* to do it:

session_start(); // only if you haven't already started the session
// make sure they have tickets in the barrel
if(!isset($_SESSION['tickets']) ¦¦ count($_SESSION['tickets']) < 1){
// put a thousand tickets in the barrel
$_SESSION['tickets'] = range(1,1000);
}
// spin the barrel every time
shuffle($_SESSION['tickets']);
// pull out a ticket
echo 'You drew number '.$_SESSION['tickets'][0];
// check the ticket to see if it's a winner
if($_SESSION['tickets'][0] == 42){
echo ' You win! :)';
} else {
echo ' Sorry, please try again. :(';
}
// throw out the ticket
unset($_SESSION['tickets'][0]);

If you want it to be per 1,000 page views regardless of user (don't forget spiders and downloaders) then you'll need to store the list of numbers in a file or database on your server.

*Don't store large amounts of data in the session as it can slow down your entire site. [webmasterworld.com] Depending on what else you are storing, anything more than a 1,000 numbers probably shouldn't use sessions (and, depending on your situation, even a couple hundred might be too many to store in sessions).