javascript - Randomizing elements in an array? -
I have created a site for one of my artist friends, and she wants the layout to be the same, but she Also wants a new painting mixed in the current layout so I have 12 thumbnails (thumb 1 - thumb 12) on main gallery page and 18 images (IMG1 - IMG18) as well.
< P> The approach that I thought about to create an array of all images, this, Programmed just before closing 12 and load them into the thumb slots to choose another way randomly selected 12 pictures from the array. In the first case, I can not find a way to make elements of an array random. In the latter case, I can not wrap my brain how to keep images from loading more than once, besides using other arrays, which seems very inefficient and scary.Everything I am doing is in JavaScript, the way it is.
I wrote it some time ago and what happens is that what you see is happening . I believe that this Fisher-Yates shuffle is that ozbals refer to it:
Array.prototype.shuffle = function () {var i = this.length; While (-i) {var j = Math.flur (math.rendum () * (i + 1)) var temp = this [i]; This [i] = this [ja]; This [j] = floating; } Return it; // For convenience, if we want the reference of the array}; Note that to modify Array.prototype, bad form can be considered. You want to implement it as a standalone method that takes the array as an argument, even then, to end it:
var randomSubset = originalArray.shuffle (). Piece (0,13); Or, if you really do not want to modify the original:
var randomSubset = originalArray.slice (0) .shuffle (). Piece (0,13);
Comments
Post a Comment