SitePoint Sponsor

User Tag List

Results 1 to 9 of 9

Thread: rand

  1. #1
    Simulation Cricketer
    Join Date
    Sep 2004
    Location
    australia
    Posts
    470
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    rand

    I don't want to have to use the database for this, and I'm consfused as to how to go about it:

    What I want to do is for 3 random letters to be chosen and outputted, but those letters do not alter until midnight, when 3 more random letters are chosen.

    Is this possible?
    Thanks

  2. #2
    SitePoint Addict fesh's Avatar
    Join Date
    Jan 2005
    Location
    Jackson, New Jersey
    Posts
    218
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    do something using time() and when time() = midnight select 3 new letters.
    Fesh
    imagine what it's going to be like

  3. #3
    SitePoint Guru mwolfe's Avatar
    Join Date
    Mar 2005
    Posts
    912
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well it seems simple, at least generating the random letters.. Not sure the best way to get a random letter.. but it would work to make an array of letters, then get a random number between 1-26, and then just echo the letter that exists at the index of the array..

    function todays_letters($num_letters) {
    $letters = array(
    'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');
    for ($i=0; $i<$num_letters; $i++) {
    $todays_letters[] = $letters[rand(1,count($letters))];
    }

    return $todays_letters;
    }



    now, after thinking about this for a minute or so, it dawned on me.. Although fesh's idea sounds right, it would only work if it were some kind of cronjob.. You would never want to check if time=something.. Maybe a date, but not an exact time. And if you had it update between 12 and 1 am, then each time someone visited the site it would update.. And if noone visited the site, then it wouldnt get updated at all. If you are running a cronjob you could have it run a script that got the letters for the day very simply. But if this is going to get triggered by a user who visits your page, then its a whole different story. And actually, its too late for me to think up a good way of doing this... i know it can't be that hard, i just can't think of a foolprrof method for it.. i must be missing something as well. I'm guessing you would have to do this.. the first person to visit the site after midnight should trigger the new letters.. but how to make this determination is beyond me..
    maybe fesh can give you some code to do that, or if you can do a cronjob it should be simple enough.

  4. #4
    Simulation Cricketer
    Join Date
    Sep 2004
    Location
    australia
    Posts
    470
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've got the letters easy enough, but I can't get the 24hr thing, and I am not familiar with cronjobs, besides knowing what they are.

  5. #5
    SitePoint Guru mwolfe's Avatar
    Join Date
    Mar 2005
    Posts
    912
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hey i thought of a good idea, this should work very well, (there may be a more efficient way).
    All you have to do is this. create a file, put the day of the month in it.. like today would be 12.

    Now, make a script that runs when a user visits your webpage, that will open that file, check if it is the same day of the month as the current day of the month, if it is, then don't do anything, otherwise, update the day in that file to the current day, and generate your new 3 letters.. As far as storing and retreiving the letters, you could use the same file, or another file, easy enough.. i couldnt think of any other way to do it (without a db).. if you need the code to do what i just said, let me know, it involves using:
    fopen : http://us3.php.net/manual/en/function.fopen.php to open the file
    fgets : http://us3.php.net/manual/en/function.fgets.php to get the contents of the first line - the number
    fwrite : http://us3.php.net/manual/en/function.fwrite.php - to update the file when needed
    and fclose (maybe a few others)

  6. #6
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this:

    PHP Code:
    // Simple list of letters
    $letters = @array(a,b,c,d,e,f,g,h,i,j,k,l,m,
                      
    n,o,p,q,r,s,t,u,v,w,x,y,z);

    // Set the random seed to today's date
    mt_srand((date('dmy')+2)*2378680);

    // Get some 'random' letters
    echo $letters[mt_rand(125)],
         
    $letters[mt_rand(125)],
         
    $letters[mt_rand(125)]; 
    You need to make sure you keep the "2378680" number secret (change it to something else), or someone will be able to guess the next day's letters!

    Douglas
    Hello World

  7. #7
    SitePoint Guru mwolfe's Avatar
    Join Date
    Mar 2005
    Posts
    912
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yeah there are a million and one ways you can make something "more random" it all depends on how important these numbers are.. i mean if they are like a "lotto" type game where there is money at stake, then people will try and cheat, but if its just for fun, and your visitors arent all computer science students, the likelyhood that they would bother trying to figure out the next days letters is highly unlikely.
    Given my problems yesterday (not sure if you read that thread , http://www.sitepoint.com/forums/showthread.php?t=271569 ), using the rand function, it would actually be pretty easy to figure out the next days letters.. But mt_rand seemed a lot more random. If its really important, you could even "randomize" the order of the letters first and use different numbers for the random seed on both, etc etc.

  8. #8
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    using the rand function, it would actually be pretty easy to figure out the next days letters..
    Well, lets see. Here is a series made using the method above, except with a different number in place of 2378680. What is the next set of letters?

    Using rand:
    Code:
    mwx
    rjn
    vvd
    bis
    gui
    lhx
    qtn
    vge
    bst
    gfj
    Using mt_rand:
    Code:
    dyd
    hmf
    fsh
    jmb
    pvp
    jes
    goq
    pho
    kvk
    fgb
    You are right that it would probably be better to use a file based system, depends what it is used for I guess Remember that someone really trying to crack this won't know what the PHP looks like, and the seed numbe can be changed whenever you like.

    Regards,
    Douglas
    Hello World

  9. #9
    SitePoint Guru mwolfe's Avatar
    Join Date
    Mar 2005
    Posts
    912
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    no actually, what i meant, was without making a random seed first it wouldnt be too difficult.. I dont know much about referse enginerring code, but i could probably figure it out.. it his very unlikely i could figure it using the method you chose above, whether you use rand or mt_rand, because i dont know that much about either algorithm.
    In my example, after 32,768 values the random generator started all over again.. i'm not sure exactly how that would work out with 3 random letters, but it should be somewhat similar.


Bookmarks

Posting Permissions

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