Results 1 to 10 of 10

Thread: Left-right slide show - Help - How to randomize?

  1. #1
    Join Date
    Apr 2006
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Left-right slide show - Help - How to randomize?

    DD script:
    http://www.dynamicdrive.com/dynamici...ightslide2.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

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    The easiest way is to sort the array randomly. Mike has a rather nice, simple Array.shuffle() method:
    Code:
    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:
    Code:
    slideimages.shuffle();
    Last edited by Twey; 05-14-2006 at 06:33 PM.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Apr 2006
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default It worked!

    Twey, Thanks. This solution worked like a charm
    dremiranda

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by Twey
    Mike has a rather nice, simple Array.shuffle() method:
    Code:
    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.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    At a guess (no more than that) some browsers' Math.random() function doesn't return a number.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  6. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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:

    Code:
    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.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  7. #7
    Join Date
    Apr 2006
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Slide Show randomizing update

    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

  8. #8
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    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.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  9. #9
    Join Date
    Apr 2006
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Twey,

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

    DRM

  10. #10
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    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.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •