Jump to content


Photo

Generating a Random Number & Calculating Attempts


  • Please log in to reply
2 replies to this topic

#1 Renegades

Renegades
  • New Members
  • Pip
  • Newbie
  • 1 posts

Posted 23 August 2006 - 06:22 AM

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:

<?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!";
}
?>

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

#2 dank

dank
  • Members
  • PipPip
  • Member
  • 12 posts
  • LocationCanada

Posted 23 August 2006 - 06:10 PM

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:
<?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');



?>



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:

<?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');




?>


Please note that I haven't tested it yet, but I am sure if there are any errors, you can work it out ;)
Andrew Danks - www.danks.ca

#3 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 23 August 2006 - 08:03 PM

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
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users