Non-Random Array Shuffle

Discussion in 'PHP' started by RobinDeanDotCom, Nov 16, 2007.

  1. #1
    I have an array such as ...

    $alpha = str_split('abcdefghijklmnopqrstuvwxyz');
    Code (markup):
    I'd like for this array to be shuffled based on some mathematic formulation.

    The numeric value to make use of is the date/hour/min such as ... YYYYMMDDHHmm (mm being minutes).

    Is there a way to do this and maximize the "random" (although non-random) look of the end result?

    My idea is to NON-randomly shuffle the array every minute but have it look as jumbled as possible ... in a way so that each minute looks nothing like the next.

    This array will be called upon by two separate locations, so I need for it to be the same within the minute (hence, non-random but shuffled).
     
    RobinDeanDotCom, Nov 16, 2007 IP
  2. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #2
    Yes it is possible, but you will need to user your own function (create one).

    Peace,
     
    Barti1987, Nov 16, 2007 IP
  3. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #3
    2 small hints:

    You can use range('a', 'z') to create the array instead. And you can probably do something tricky with usort(). I'll have a look at this tomorrow if no one got a working answer until then.
     
    nico_swd, Nov 16, 2007 IP
  4. RobinDeanDotCom

    RobinDeanDotCom Peon

    Messages:
    25
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks for the hints.

    Regarding hint number 1, My string has changed a bit. It's now "abcdefghijklmnopqrstuvwxyz0123" ... note the 0123 on the end.

    Is there a way to put this into a range or is it as efficient as it can be?
     
    RobinDeanDotCom, Nov 17, 2007 IP
  5. decepti0n

    decepti0n Peon

    Messages:
    519
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    0
    #5
    array_merge( range('a', 'z'), range(0,3) );
    PHP:
     
    decepti0n, Nov 17, 2007 IP
  6. kemus

    kemus Guest

    Messages:
    487
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Just have to use srand:
    
    <?
    srand(round(time()/60)); // This gets the current time in seconds since 1/1/1970, then divides by 60 (converting to minutes) and then rounds it, effectively giving you a number that changes once a minute. It then seeds the random number generator with this number. (How this works is that if you seed it with 1 for example, it will always output the same random number)
    $array = array_merge( range('a', 'z'), range(0,3) ); // This line shamelessly stolen from decepti0n
    shuffle($array); // This mixes the array randomly, with the addition of the seeded random number generator
    print_r($array); // this line prints out the array so you can see it work.
    ?> 
    
    PHP:
    PM me and let me know how it works out or if you have any problems.
     
    kemus, Nov 17, 2007 IP
  7. RobinDeanDotCom

    RobinDeanDotCom Peon

    Messages:
    25
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    It's getting better.

    What I'm trying to do is a) use a no-store, expiring php document to point to my mp3 files (done and working). b) have a "validate=" query encryption variable script. c) run it as a time based url validator that basically says "if it's been more than one second, you get low bandwidth audio instead of crystal-clear hi bandwidth." (figuratively).

    I've scaled back a little and my function is now as such ...

    function encrypt() {
    
    $numeric = str_split(time() * 1500); // obscure the use of time() by multiplying it against some preferred mathematic value.
    $alpha = range('a', 'j'); // 9 letters to replace the individual $numeric values.
    
    srand(time()); // create a new random method for the shuffle order.
    shuffle($alpha); // shuffle the 9 letter key every second (can't crack the encryption until you crack key, which is always changing).
    
    $composite = array(); // blank array to send resulting values to.
    
    foreach($numeric as $value) {
    
    $value = $alpha[$value]; // replace any value in $numeric with whatever value is held in the same place by $alpha.
    if (rand(0, 1) == 1) $value = strtoupper($value); // randomize the letter case so that base_64 often appears differently.
    array_push($composite, $value); // place each result into the $composite array.
    
    }
    
    echo(substr(base64_encode(implode($composite)), 0, -2)); // base_64 encode the random, time based value but remove the equal signs.
    
    }
    
    encrypt();
    PHP:
    I'm still working on the decryption script. Does this look, as far as mid-grade file protection is concerned, decent? Are there any parts that might be made more efficient?
     
    RobinDeanDotCom, Nov 19, 2007 IP
  8. RobinDeanDotCom

    RobinDeanDotCom Peon

    Messages:
    25
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    It's getting worse LOL.

    Now I'm trying something much more simple.

    Encrypting "seems" to be working but decrypting is not. I'm trying to keep the key random every second. What's wrong here?

    <?php
    
    echo('Time: '.time().'<br><br>');
    
    $values = array_merge(range(0, 9), range('A', 'Z'), range('a', 'z'));
    $key = $values; srand(time()); shuffle($key);
    
    define(A, implode($values));
    define(B, implode($key));
    
    function encrypt() {
    echo('Encrypted: '.strtr(time(), A, B).'<br><br>');
    }
    
    encrypt();
    
    function decrypt() {
    echo('Decrypted: '.strtr($_GET['validate'], B, A));
    
    }
    
    decrypt();
    
    ?>
    PHP:
     
    RobinDeanDotCom, Nov 20, 2007 IP