Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 10 of 10
  1. #1
    Senior Member assassingod's Avatar
    Join Date
    Nov 2002
    Posts
    112
    Member #
    307
    Hey; I am working on something that requires 6 numbers to update everyweek. I know how to make a php script so numbers update each time the page is displayed, but I am finding it dificult to make it so it changes everyweek. Can someone explain how to do this.
    Here is my random number script:
    PHP Code:
    <?php
        srand
    microtime()   *   1000000);
        
    $one rand(1,100);
        
    $two rand(1,80);
        
    $thr rand(1,60);
        
    $fou rand(1,40);
        
    $fiv rand(1,20);
        
    $six rand(1,10);
        echo(
    "<b>This week's Numbers:</b> ".$one.",
                "
    .$two.", ".$thr.", ".$fou.", ".$fiv.", ".$six."<BR>");
    ?>
    I think its something to do with :
    PHP Code:
        srandmicrotime()   *   1000000); 
    but im not totally sure

    Anyone shed some light on this?

    edited to not break table layout
    assassingod - vB.org Moderator :ichatsmile:

  2.  

  3. #2
    Senior Member filburt1's Avatar
    Join Date
    Jul 2002
    Location
    Maryland, US
    Posts
    11,774
    Member #
    3
    Liked
    21 times
    Well there are two possibilities:

    1. Run a cron job that makes random numbers every week and dumps them in a database or file, then just use those numbers (best solution)
    2. Rewrite the number generator in the sense that it would use the week as a seed

    Does your server support cron jobs?
    filburt1, Web Design Forums.net founder
    Site of the Month contest: submit your site or vote for the winner!

  4. #3
    Senior Member filburt1's Avatar
    Join Date
    Jul 2002
    Location
    Maryland, US
    Posts
    11,774
    Member #
    3
    Liked
    21 times
    BTW with PHP you can do this echo instead:
    Code:
    "<b>This week's Numbers:</b> $one, $two, $thr, $fou, $fiv, $six<BR>"
    filburt1, Web Design Forums.net founder
    Site of the Month contest: submit your site or vote for the winner!

  5. #4
    Senior Member assassingod's Avatar
    Join Date
    Nov 2002
    Posts
    112
    Member #
    307
    Thanks for that.

    Well, yes and no, I am currently testing this code locally, but my host does support Cron Jobs, though I dont want to keep uploading it all the time.

    Since I am new to PHP, you'll have to explain both possibilities more thoroughly
    assassingod - vB.org Moderator :ichatsmile:

  6. #5
    Senior Member filburt1's Avatar
    Join Date
    Jul 2002
    Location
    Maryland, US
    Posts
    11,774
    Member #
    3
    Liked
    21 times
    Cron job: basically a script that runs at a given time and frequency; cron jobs can be PHP scripts
    Redoing the RNG: making some algorithm that generates with a high probability of uniqueness a set of six numbers given the week (1 to 52).

    Cron job is much easier and if done properly much faster too because you're essentially caching the numbers, not constantly regenerating them.

    ...although I've never done a cron job before but the script should be easy.
    filburt1, Web Design Forums.net founder
    Site of the Month contest: submit your site or vote for the winner!

  7. #6
    Senior Member assassingod's Avatar
    Join Date
    Nov 2002
    Posts
    112
    Member #
    307
    Thank you Could you explain the secon suggestion aswell please? It will be appreciated
    assassingod - vB.org Moderator :ichatsmile:

  8. #7
    Senior Member filburt1's Avatar
    Join Date
    Jul 2002
    Location
    Maryland, US
    Posts
    11,774
    Member #
    3
    Liked
    21 times
    It's kinda hard to explain...unless somebody who understands my ramblings would like to jump in
    filburt1, Web Design Forums.net founder
    Site of the Month contest: submit your site or vote for the winner!

  9. #8
    Senior Member assassingod's Avatar
    Join Date
    Nov 2002
    Posts
    112
    Member #
    307
    Its ok I'll take alook at Cron Jobs, see where it gets me. Thanks
    assassingod - vB.org Moderator :ichatsmile:

  10. #9
    Junior Member artesea's Avatar
    Join Date
    Jul 2003
    Location
    Hemel Hempstead, UK
    Posts
    1
    Member #
    2216
    Sorry to bump this back up, but i found this tread via google, and just had to lend a hand

    Like you said the problem was with
    PHP Code:
    srandmicrotime()   *   1000000); 
    random generators aren't random, they just start at a point along a long line of number, and then read them out one by one.

    by using srand you make it start at a certain point.
    in this case at the current number of milli-seconds microtime() multiplied by 1000000.
    this is normal ok, as most people are unable to guess exactly what time the script was run.

    Now what you are looking for is something which will only change weekly eg.
    Week 1: 56, 73, 32, 26, 13, 03
    Week 2: 06, 45, 57, 34, 09, 01

    all you need to do, is ensure that your random seed starts at the same place, each run of the script, during the week. the easiest way is to use
    PHP Code:
    sranddate("W")); 
    this will start the random seed based on the current week number

    flaws with this system is that it isn't too hard to crack, and also it repeats the numbers each year, so something like this will be slightly better
    PHP Code:
    sranddate("WY6858W") * 2186432156); 
    what i've done here is to generat the seed based on the Week # and the Year, and the number 6858 and Week # again and then multiplied it by some other number.

    this will ensure that the numbers only change when the week changes.

    artesea

  11. #10
    JR
    JR is offline
    Senior Member JR's Avatar
    Join Date
    Nov 2002
    Location
    UK
    Posts
    4,354
    Member #
    257
    Welcome and thanks for the input
    JR


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 05:09 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com