PDA

View Full Version : Left-right slide show - Help - How to randomize?



dremiranda
05-14-2006, 06:03 PM
DD script:
http://www.dynamicdrive.com/dynamicindex14/leftrightslide2.htm

Does someone have a "randomizing" script variation for this slide show? I would like the show to start on a different slide each time. I read through the forum, but could not find anything. Thanks

Dremiranda

Twey
05-14-2006, 06:28 PM
The easiest way is to sort the array randomly. Mike has a rather nice, simple Array.shuffle() method:
Array.prototype.swap = function(x, y) {
var t = this[x]; this[x] = this[y]; this[y] = t;
};
Array.prototype.shuffle = function() {
var i = this.length;
while(i--) this.swap(i, Math.floor((Math.random() % 1) * (i + 1)));
};Just call that after you've established your list:
slideimages.shuffle();

dremiranda
05-14-2006, 08:29 PM
Twey, Thanks. This solution worked like a charm
dremiranda

jscheuer1
05-15-2006, 12:54 AM
Mike has a rather nice, simple Array.shuffle() method:
Array.prototype.swap = function(x, y) {
var t = this[x]; this[x] = this[y]; this[y] = t;
};
Array.prototype.shuffle = function() {
var i = this.length;
while(i--) this.swap(i, Math.floor((Math.random() % 1) * (i + 1)));
};

'Nice' I will give you. I will even give you 'simple to use' but, simple? No. When Mike first put this forward in these forums I could see that it worked but, not only couldn't I see why, I could barely read it. I've come a fair ways since then in my comprehension of such things and I now get it but, one thing is still puzzling me. Why take modulus 1 Math.random()? Math.random() already returns a number less than one, therefore modulus 1 of it will always equal the number it was before that.

Twey
05-15-2006, 01:20 PM
At a guess (no more than that) some browsers' Math.random() function doesn't return a number.

jscheuer1
05-15-2006, 03:31 PM
I doubt that, because multiplying a string that is also a number would have the same effect and is the next action taken, and in the later:


array.sort(function() {return 0.5 - Math.random();}) //thanks to Mike (aka Mwinter) :)

We see none of that in a situation where a string would return an error. I also find it hard to believe that any Math.method() would return anything other than a number unless it started with something NaN which Math.random() by itself cannot do.

But, anything is possible, in theory, where legacy browsers are concerned. My NS4 and IE4 didn't have a problem identifying typeof Math.random() as a number though. We may have to await clarification from Mike but, I think it is just a holdover from other (evolving?) code where it did serve a purpose.

dremiranda
05-17-2006, 04:34 PM
Further to this thread. The script works, but it tends to capture slide 0 and then starts a sequence as follows: slide 0, random slide, slide 0, random slide, etc.

The script can be seen working at http://www.kyotosushibar.com. You have to be patient (there are 26 slides) but when slide 0 comes up (its the restaurant logo)then the abnormal sequence starts.

For this particular application, maybe the best alternative would be to randomize the FIRST slide only, and then start the normal sequence. Does anyone know a way of doing this? (or repairing the randomizong script?)

Thanks

Twey
05-17-2006, 06:14 PM
Honestly, it doesn't happen. I've sat and watched it for twenty minutes solidly. It doesn't repeat 0 any more than it ought. :)

dremiranda
05-17-2006, 09:40 PM
Twey,

Thanks, It must be my computer or just a glitch. You have done much more than needed!

DRM

Twey
05-17-2006, 09:42 PM
Could be your browser -- I'm using Firefox -- but I see no reason why this should happen, I've looked over the code several times.