Results 1 to 4 of 4

Thread: Random Content Order script not so random?

  1. #1
    Join Date
    Apr 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Random Content Order script not so random?

    1) Script Title: Random Content Order script

    2) Script URL (on DD): http://www.dynamicdrive.com/dynamici...ntentorder.htm

    3) Describe problem:

    I have 16 divs that need to be randomized. I have tried this script, but in my experience so far, it doesnt seem that its really all that random. If i reload the page a couple of times, the ones that are last in the HTML are uasually still last when randomized.

    Is this a weakness with this script, or is there another explanation?

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    29,167
    Thanks
    44
    Thanked 3,240 Times in 3,201 Posts
    Blog Entries
    12

    Default

    Well random means random, not unique. And it's true that javascript's built in Math.random() object isn't truly random. It tends to fall into predictable patterns, and so only gives the appearance of randomness to the casual observer. If that's all that's required though, it's a fine thing to use. You can add cookies if you want to eliminate certain types of sequential repetition, and I believe that elsewhere in these forums there is at least one thread on creating a truly or a more nearly true random number generator.

    A lot depends upon how mission critical this is, what the user experience will be like, things like that. For instance, what if the user has javascript off? In that case only a server side language would help.

    Notes: In fact different browsers seem to handle Math.random() differently, but they all tend to fall into patterns, just different ones. Here's that thread I was thinking of:

    http://www.dynamicdrive.com/forums/s...573#post125573
    - John
    ________________________

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

  3. #3
    Join Date
    Apr 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Yes, I realize that random isnt the same as unique. The problem i am having is that it pretty much seems like about half of the time the script places the items in pretty much the same order. It might move one or two items up one, but out of 20 items, the first 10 in the code are shown first and the last are shown at the bottom 50% of the time.

    I really dont require true randomness, but it doesnt seem like this script works half of the time, and does not in any way give an apperance of randomness.

    Try it with 20 items, for example 10 red boxes at the top of the code and 10 grenn boxes at the bottom - refresh the page a few times. Can anyone else reproduce this error, or is it just me?

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    29,167
    Thanks
    44
    Thanked 3,240 Times in 3,201 Posts
    Blog Entries
    12

    Default

    As I say it depends upon the browser. It also depends to a degree upon the 'luck of the draw'. How you code the colors is also important. At first I tried a dual class name approach:

    Code:
    <style type="text/css">
    .group1 {
    visibility: hidden;
    background-color: red;
    }
    .green {
    background-color: green!important;
    }
    </style>
    with markup like:

    Code:
    <div class="group1">
    Content 2
    </div>
    and for green (dual class highlighted):

    Code:
    <div class="group1 green">
    Content 2
    </div>
    In that case, the colors never changed, though the content did. Then I tried a slightly different approach for the green ones:

    Code:
    <div class="group1">
    <div class="green">
    Content 2
    </div>
    </div>
    Now the colors went with their content. Randomness appeared more genuine in IE, but FF looked fairly random as well.
    - John
    ________________________

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

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
  •