Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Jun 2008
    Posts
    104
    Thanks
    71
    Thanked 0 Times in 0 Posts

    Question 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!

  • #2
    New Coder
    Join Date
    Feb 2010
    Posts
    20
    Thanks
    2
    Thanked 2 Times in 2 Posts
    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:

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

  • #3
    New Coder
    Join Date
    Feb 2010
    Posts
    20
    Thanks
    2
    Thanked 2 Times in 2 Posts
    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
    }
    ?>

  • #4
    Regular Coder
    Join Date
    Jun 2008
    Posts
    104
    Thanks
    71
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Courtney View Post
    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?

  • #5
    New Coder
    Join Date
    Feb 2010
    Posts
    20
    Thanks
    2
    Thanked 2 Times in 2 Posts
    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?

  • #6
    Regular Coder
    Join Date
    Jun 2008
    Posts
    104
    Thanks
    71
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Courtney View Post
    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.

  • #7
    New Coder
    Join Date
    Feb 2010
    Posts
    20
    Thanks
    2
    Thanked 2 Times in 2 Posts
    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
    •