Jump to content

[SOLVED] Generate random number daily.


PP-Stuart

Recommended Posts

Hello, just goin through a book on how to program in php and for a site I am making I need to create a script that generates a random number every day.

 

I have written this so far and unfortunately it is not giving me the results I need.

 

<?php
$rotate = true;
$vardate = $_POST["textfield"];
//$vardate = date(j, time());

if ($rotate) {
$currentday = date(j, time());
$test = rand(1,100);
$rotate = false;
}

if ($currentday != $vardate) {
$rotate = true;
}

echo ("today is $vardate, and the test date is $currentday, and the random number is $test");
?>
<form id="form1" name="form1" method="post" action="<?php echo $_SERVER[php_SELF] ?>">
  <input type="text" name="textfield" />
  <input type="submit" name="submitbutton" />
</form>

 

basically, the issue is that when the dates do not equal, signaling a change in the date, I want the variable $test to output a random number.

 

What ends up happening is that it spits out a random number, and then turns null on the next page refresh.  I am pretty sure I am going about this completely incorrectly haha, but I am in the learning phase.  Any help would be totally sweet, I appreciate it.

 

Link to comment
Share on other sites

Well you can't just refresh the page. For the php script to work properly you have to keep inputing the data information in the text field as it is $_POST. If that is the problem than you can put it into the $_GET or even better, $_SESSION.

 

If that's not the problem, please clarify. I'm a bit confused.

Link to comment
Share on other sites

Ok I only had the form up as a way to test how the script reacts to the changing of the numbers.  So here is exactly what I want it to do.

 

I want the $test variable to be a random number, and have it stay that same number for one whole day.  Upon changing of the date, I want it to change to another random number, but only once, and have it stick for another 24 hours.

 

The two if statements were the best my primitive brain could come up with as a way to change $test based on a changing of the date, but it seems the variable does not stick.  Does this make any sense lol?

Link to comment
Share on other sites

ok I tried that route but there must be something I am missing as I am not getting the correct results.

 

so for the below code, I want to enter a number to the form, and when I enter a different number I want the random number to change, but if I enter the same number it should stay the same...  Here is what I have.

 

<?php
$rotate = true;
$vardate = $_POST["textfield"];
touch("currentdate.txt");
$filename = "currentdate.txt";
//$vardate = date(j, time());

if ($rotate) {
//if the date has changed, create a new random number to the variable $test
($cf = fopen($filename, "w")) or die("The file could not be opened, please check your directory permissions");
fwrite($cf, $vardate);
fclose($cf);
$cf = fopen($filename, "r");
$currentday = fread($cf, 3);
fclose($cf);
$test = rand(1,100);
$rotate = false;
}

if ($currentday != $vardate) {
$rotate = true;
}

echo ("today is $vardate, and the test date is $currentday, and the random number is $test");
?>
<form id="form1" name="form1" method="post" action="<?php echo $_SERVER[php_SELF] ?>">
  <input type="text" name="textfield" />
  <input type="submit" name="submitbutton" />
</form>

 

it is live here if you want to see what it does http://www.stuartkrempin.com/dailyrotate2.php

Link to comment
Share on other sites

There is a far easier way since the random generator it's not really random. It's just some fancy math applied on a seeded number. Random generators simulate "randomness" using very fast changing seeds, like the microseconds on the clock, thus the value outputed changes every time the seed changes. If you supply a constant seed, the random generator gets stuck.

So if you seed the random generator with the current date, voilá, problem solved.  ;D

 

<?php
$seed = date("d"); //get the day.
srand($seed); //seeds the random generator with it.
echo $rand(); //get the not so random number 
?>

 

That way the rand() function will output the same number until the day changes. You can also specify ranges in the rand() function, it won't affect the behavior through the day.

 

Be aware though, that the number WILL repeat itself over the months (ie. 25th September will return the same number as the 25th of December). You can overcome that problem by appliying some extra math and dates to the seed. Like $seed = $day+$month+$year;

 

Hope it helps, cheers.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.