four0four
02-23-2010, 11:34 PM
Can anyone figure out what I'm doing wrong?
I'm trying to increase a number based on a timestamp. The number increases each second by adding a random number from 1 to 10.
The number increases and then decreases, but how can I stop it from decreasing?
<?php
$start_time = 1266967333; //Start time timestamp
$number_start = 100; //Start value of number
$random_increase = mt_rand(1,10); //Increments per second
echo floor($number_start+(((time()-$start_time)/1)*$random_increase));
?>
Thanks!
Courtney
02-24-2010, 01:58 AM
The number decreases because you run the script with a random number and then run it again with a different random number.
You are not incrementing anything as there is no loop.
Add:
echo $random_increase;
And you'll see that the lower the random the lower the number.. the higher the higher.
Courtney
02-24-2010, 02:10 AM
This code increments by the same random number 1 times a second for 101 seconds.
<?php
$start_time = 1266967333; //Start time timestamp
$number_start = 100; //Start value of number
$random_increase = mt_rand(1,10); //Increments per second
for ($x = 1; $x <=100; $x++) { //loops until x=101
$answer = floor($number_start+(((time()-$start_time)/1)*$random_increase));
echo $answer. "<br>";
sleep(1); //pause 1 second
}
?>
four0four
02-24-2010, 09:37 PM
This code increments by the same random number 1 times a second for 101 seconds.
<?php
$start_time = 1266967333; //Start time timestamp
$number_start = 100; //Start value of number
$random_increase = mt_rand(1,10); //Increments per second
for ($x = 1; $x <=100; $x++) { //loops until x=101
$answer = floor($number_start+(((time()-$start_time)/1)*$random_increase));
echo $answer. "<br>";
sleep(1); //pause 1 second
}
?>
Thanks for the help! I tried your example and it still doesn't seem to work.
So I guess it's impossible, since as you mentioned it uses a different number each time?
Courtney
02-24-2010, 10:28 PM
Using that code on my webserver produces a working result. It looks like this:
166202
166204
166206
166208
166210
If I refresh the page it gets a different random number and gives a different result but increments using that number. Is that what you are looking for?
four0four
02-24-2010, 11:40 PM
Using that code on my webserver produces a working result. It looks like this:
166202
166204
166206
166208
166210
If I refresh the page it gets a different random number and gives a different result but increments using that number. Is that what you are looking for?
Yeah, that's exactly what I'm looking for.
Basically, just a way to increment the number by adding a random number from 1-10.
For example:
166202
166207
166208
166210
166218
It always increases (the reason I'm using a timestamp), but randomly increases without following any particular pattern.
Thanks again for your help.
Courtney
02-25-2010, 12:27 AM
Try this:
<?php
$number = 100; //sets minimum first number
for ($y = 0; $y <= 100; $y++) { //loops until $y = 101
$z = mt_rand(1,10); //generate a random nuber between 1 and 10
$number = $number + $z; //add the last number displayed to the random number
echo $number . "<br />"; // print number to screen
}
?>