Jump to content

Archived

This topic is now archived and is closed to further replies.

Renegades

Generating a Random Number & Calculating Attempts

Recommended Posts

Here's what I'm trying to do... Im trying to generate random number and if that persons # matches one of the winners then it says they win blah blah. However, I want to limit the amount of attempts someone can try, and the total number of tries
per day and have it unset both values (the total_number_of_tries for the day and the_persons_tries_today).
Also, If someone gets a winning #, I want that to be written so like it calculates the total number of winners today and its removed from the possible winning numbers.


I'm guessing this will take mysql so each IP should have stats like this:
attempts: 43
wins: 0

and then it resets each day.

If someone reaches their max amount of tries then they can't play anymore. If someone wins, then it adds a win to their mysql table and removes the variable he won ($winner1 or $winner2 or whatever number he got that was a winning value).

Right now -- I want it set to: Max attempts a day: 30000
My current code:

[code]
<?php
$winner1 = 234;
$winner2 = 630;
$winner3 = 550;

srand ((double) microtime( )*1000000);
$random_number = rand(0,10000);
echo "Your Number: $random_number<br>";

if ( $random_number == "$winner1" ) {
  echo "Congratulations, you're a winner!";
} elseif($random_number == $winner2){
echo "Congratulations, you're a winner!";
} elseif($random_number == $winner3){
echo "Congratulations, you're a winner!";
} else {
  echo "Sorry ,you're not a winner!";
}
?>
[/code]

If need be, i can pay $2 for this done via paypal :)

Share this post


Link to post
Share on other sites
Hi, I put this together for you.

first, create a folder called "ips". chmod it to 0777.


Here is the code I re-did for you:
[code]
<?php
$winner1 = 234;
$winner2 = 630;
$winner3 = 550;

$check = file_get_contents("ips/".$_SERVER[REMOTE_ADDR].".txt");
if($check<30000){
srand ((double) microtime( )*1000000);
$random_number = rand(0,10000);
echo "Your Number: $random_number<br>";

if ( $random_number == "$winner1" ) {
  echo "Congratulations, you're a winner!";
} elseif($random_number == $winner2){
echo "Congratulations, you're a winner!";
} elseif($random_number == $winner3){
echo "Congratulations, you're a winner!";
} else {
  echo "Sorry ,you're not a winner!";
}

$f0 = fopen("ips/".$_SERVER[REMOTE_ADDR].".txt","w+");

$f1 = fwrite($f0,$check+1);
if(!is_writable('ips/'.$_SERVER[REMOTE_ADDR].'.txt')){ chmod("ips/".$_SERVER[REMOTE_ADDR], 777); }

$f2 = file_get_contents('ips/all.txt');
if(!ereg($_SERVER['REMOTE_ADDR'],$f2) OR (!file_exists('ips/all.txt'))){
$f3 = fopen("ips/all.txt","w+");
$f4 = fwrite($f3,$f2."+".$_SERVER[REMOTE_ADDR]);
if(!is_writable('ips/all.txt')){ chmod("ips/all.txt", 777); }
}



}
else
{
echo "Sorry, you have used your 30000 chances.";

}

?>

<?php

#cron.php

$f0 = explode('+',substr(file_get_contents('all.txt'),'1'));

for($i=0;$i<$f0;$i++){

unlink($f0[$i].'.txt');

}

unlink('all.txt');



?>
[/code]



Now, you are going to need to create a cron tab:
0 0 * * * php /path/to/ips/cron.php
The cron tab will run every morning at 12am.


Here is the code for cron.php:

[code]
<?php

#cron.php

$f0 = explode('+',substr(file_get_contents('all.txt'),'1'));

for($i=0;$i<$f0;$i++){

unlink($f0[$i].'.txt');

}

unlink('all.txt');




?>
[/code]


Please note that I haven't tested it yet, but I am sure if there are any errors, you can work it out ;)

Share this post


Link to post
Share on other sites
You don't need a cron job. Just store the date of last attempt.

If stored_date == today
  increment attempts
else
  set stored_date date to today
  set attempts to 1
end if

if attempts <= limit
    process
end if

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.