Page 1 of 3 123 LastLast
Results 1 to 10 of 24

Thread: Random without Math.random

  1. #1
    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 Random without Math.random

    I recently discovered, quite by accident, that one can create apparent randomness in a script without using Math.random. It happened because I had created an unbalanced system. The script starts out sliding 6 images into 6 divisions using the same OO script. It is an OO adaptation of a slide in slide show so, the script continues to slide in images at the same regular interval for all divisions. However, I set up a check to make sure no other image was sliding whenever a particular one was about to, if it was, the new slide would be delayed 100ms and try again. The interval was 3000ms so I figured things would just go in order but, they do not always do so and after a while appear to become quite random. I'm not concerned about this as a problem, rather as an unexpected 'feature'. I am wondering if others have or have seen other examples of this randomness in scripts without using Math.random?
    - John
    ________________________

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

  2. #2
    Join Date
    Aug 2004
    Posts
    10,143
    Thanks
    3
    Thanked 1,008 Times in 993 Posts
    Blog Entries
    16

    Default

    Well I do know setTimeout() and setInterval() do not always fire per the time interval you specify, probably due to system resources issues at the time of running with your computer. But how does this replace Math.random though if one needs to, say, generate a random number in JavaScript?

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

    Default

    This is known as a race condition. It's quite common in thread-based applications, and various solutions exist for it (such as mutexes).

    ddadmin: it's not to do with system resources, but rather that the browser is single-threaded. Thus, if some other request is being processed at the time the timeout expires, its execution is postponed.
    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!

  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

    This apparent randomness does not of itself (though, with modification/clever use, might) replace generating a random number but, can replace the need for the random number to begin with. It depends upon why the random number was needed. Oftentimes, a random number is only used to generate random effects. If this is all that is desired, the number isn't required, just the effect.

    Perhaps a demo is in order:

    http://home.comcast.net/~jscheuer1/s...ete_nosync.htm
    - John
    ________________________

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

  5. #5
    Join Date
    Aug 2004
    Posts
    10,143
    Thanks
    3
    Thanked 1,008 Times in 993 Posts
    Blog Entries
    16

    Default

    Quote Originally Posted by jscheuer1 View Post
    Very cool demo, and I'm just talking about in general! Back to how it relates to the discussion though, the only thing that's uneven in the demo for me is the delay between image changes with supposedly identical slideshow instances. Everything else looks uniform, such as the sequence of images that get slided in. What effect are you referring to as far as not requiring Math.random() to generate?

  6. #6
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    That demo just proves that there is in fact a pattern behind the "randomness". While the images appear, after a few minutes, to be at random, watching the script progress from the beginning and careful inspection of several images in a row as it continues shows a clear and defined pattern, in which one step in the rotation is skipped every few times dependant on what the browser is currently occupied with.

    If you scramble a number several times, you could call that "random". In fact, any random function does just that, so this is just an example of a way to fake a PSEDUO-random number. (Or, I suppose, event.)

    The main problem that I see with it's use is the repetition involved. One could, I suppose, run the function several hundred times and hope for a randomized result, but that seems innefficient. Additionally, I also wonder if there is anything actually random about the script at all, since it might do the same exact thing each time. For example, md5 is a very complex algorithm that generates what looks like it must be random, but there is a method to the madness, as there would be here.

    Perhaps you should write a test page that cycles through a similar function say 1000 times then outputs the value (or pictures, whatever), and we can see if it is, as I suspect, the same, or at least the same on many occasions.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  7. #7
    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 ddadmin View Post
    Very cool demo, and I'm just talking about in general! Back to how it relates to the discussion though, the only thing that's uneven in the demo for me is the delay between image changes with supposedly identical slideshow instances. Everything else looks uniform, such as the sequence of images that get slided in. What effect are you referring to as far as not requiring Math.random() to generate?
    Thanks on the demo. I just put it up mostly as I had made it, without ever intending it to be random. The only change I made was to allow this 'random' behavior in IE. Originally, I was only caching slide instances in other browsers as, IE is capable of doing 6 at once smoothly. So is Opera but, I hadn't realized that at first. FF got rough even with two at once.

    On to a further explanation of the randomness. If you reload the browser, you will probably get different results than a first time through. Especially if you occasionally clear the cache. This is due to the fact that this script is also written to not start a given sliding slide show until all of its images are preloaded (a slightly different thing than cached but, using the cache). What this does is introduce different start times. That could be hard coded though, as could different intervals.

    Anyways, my object wasn't to create an unstable system but, that is what happened, at least somewhat. My point is that one could probably create an even more unstable system intentionally and get even more striking results. On occasion, if you let the thing run long enough, it gets really random. It will go along in a groove for awhile but then, sometimes, devolves into what looks like total randomness.

    My question was, "Has anyone seen or written other scripts that do this, without using Math.random?"
    - John
    ________________________

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

  8. #8
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    I haven't seen anything like it.

    The problem, as I said above, is that for this to work, you need to allow the script to run for some time before it really becomes 'random'.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  9. #9
    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

    There is no problem djr33, we are discussing a phenomenon.
    - John
    ________________________

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

  10. #10
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    A phenomenon is by definition unexplained. Single threated processes explain this.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

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
  •