Pull Random records from JSON using Javascript

I'm trying to pull information from a json file.  The goal is to get 9 records returned in random order.
My data looks like this (there are 60 different videos):
{"videos":[
    {"id":"1", "ytID":"ys7Xnw34T5A", "title":"Video 1", "category":"Category 1"},
    {"id":"2", "ytID":"HlrO8gH7AEY", "title":"Video 2", "category":"Category 2"},
    {"id":"3", "ytID":"NzYp5YGK9vc", "title":"Video 3", "category":"Category 3"},
    {"id":"4", "ytID":"brrTb2zmOYd", "title":"Video 4", "category":"Category 4"}
]

Open in new window


I pull the data like this:

$.getJSON(url, function(data){
     //GET THE FIRST VIDEO
     var firstVideo = data.videos[0].title;
     var firstVideoCode = data.videos[0].ytID;
     
     getVideo(firstVideoCode);
     
     var x = 0;
     $.each(data.videos, function(index, videos) {

          if (x < 10){
                //create blockText to display results 
                blockText += '<div class="thumbVids">\
		<a href="#" onclick="getVideo(\'' + videos.ytID + '\')">\
		<img class="vidThumb" src="//i1.ytimg.com/vi/' + videos.ytID + '/hqdefault.jpg" border="0" /></a>\
		<p class="title">' + videos.title + '</p>\
		</div>';
		}
          x++;
       });
});	

Open in new window


I'm not sure the best way to grab a random sampling.  Any suggestions?  Thanks for your help!
LVL 1
slightlyoffAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Duy PhamFreelance IT ConsultantCommented:
You can try to shuffle your video array before creating blockText for all the videos.
A sample shuffle function based on Fisher and Yates algorithm:
function shuffleArray(arr) {
   var swap = function(i, j) { var tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp; };
   
   for (var i = arr.length - 1; i > 0; i--) {
       int j = rand.Next(i + 1);
       swap(i, j);
   }
}

Open in new window

And then you just need to add up blockText with each videos in the array after shuffling:
      shuffleArray(data.videos);
      $.each(data.videos, function(index, videos) {
          //create blockText to display results 
          blockText += '<div class="thumbVids"><a href="#" onclick="getVideo(\'' + videos.ytID + '\')"><img class="vidThumb" src="//i1.ytimg.com/vi/' + videos.ytID + '/hqdefault.jpg" border="0" /></a><p class="title">' + videos.title + '</p></div>';
      });

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
slightlyoffAuthor Commented:
Thank you for your help!  Perfect!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.