Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Increase number from start date

1. ## Increase number from start date

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?

Code:
```<?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!

• 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.

Code:
`echo \$random_increase;`
And you'll see that the lower the random the lower the number.. the higher the higher.

• This code increments by the same random number 1 times a second for 101 seconds.

PHP Code:
``` <?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 } ?> ```

• Originally Posted by Courtney
This code increments by the same random number 1 times a second for 101 seconds.

PHP Code:
``` <?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?

• 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?

• Originally Posted by Courtney
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.

• Try this:

PHP Code:
``` <?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     } ?> ```

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•