JQuery Random Lists

I was looking for a JQuery code that would display a random order of unordered list. The HTML markup is displayed below. Each time the page is loaded, a different order of list is displayed.

<ul>
<li>one</li>
<li>two</li>
<li>three</li>
<li>four</li>
</ul>

Here’s the piece of JQuery code that you’ll need add to your webpage to sort and randomize your unordered list.

<script>$(document).ready(function(){
$(‘ul’).each(function(){
// get current ul
var $ul = $(this);
// get array of list items in current ul
var $liArr = $ul.children(‘li’);
// sort array of list items in current ul randomly
$liArr.sort(function(a,b){
// Get a random number between 0 and 10
var temp = parseInt( Math.random()*10 );
// Get 1 or 0, whether temp is odd or even
var isOddOrEven = temp%2;
// Get +1 or -1, whether temp greater or smaller than 5
var isPosOrNeg = temp>5 ? 1 : -1;
// Return -1, 0, or +1
return( isOddOrEven*isPosOrNeg );
})
// append list items to ul
.appendTo($ul);
});
});</script>

In addition, you can add a class to your unordered list like the example below:

<div class=”sample”>
<ul>
<li>…</li>
<ul>
</div>

You will need to change this line to include the class.

$(‘.sample ul’).each(function(){

Generating PHP Random Keys

There are many ways of generating random keys in PHP. Random keys are essential in applications where you need unique identifiers that are not necessarily in sequential order. A prime example of where to use this script would be is when you need to create unique key for a URL shortener script.

For example: a user supplies a long URL. The script generates a random key based a predetermined length and returns a short URL based on the random key. So, here are  a couple of PHP random scripts that you might find useful. They both generate 6 random characters each time they are called.

Example 1

We create a function called rand_str(). We set the characters and generate a random key with a length of 6 characters. We make sure the same two characters don’t appear next to each other.

function rand_str($length = 6, $chars = ‘ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890?) {
  $chars_length = (strlen($chars)1); // Length of character list
  $string = $chars{rand(0, $chars_length)}; // Start our string
  for ($i = 1; $i < $length; $i = strlen($string)) { // Generate random string
    $r = $chars{rand(0, $chars_length)}; // Grab a random character from our list
    if ($r != $string{$i1}) { 
      $string .=  $r; // Make sure the same two characters don’t appear next to each other
    } 
    echo $string; // Return the string
  }
}

Example 2

This is example is essentially the same as above, but a bit more succinct. It generates a 6 character random string, but it doesn’t have a consecutive character feature like above.

for ($s =, $i = 0, $z = strlen($a = ’0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ’)-1; $i != 6; $x = rand(0,$z), $s .= $a{$x}, $i++);
$key = $s;
echo $key;

There you have it. Two scripts for generating random strings based on a predetermined length. It’s perfect for applications that need to generate unique and random keys.

There’s a good possibility a “swear” word could be generated by random chance. To avoid such words, simply take out the vowels from your character list.