The forums have permanently moved to This forum will be kept around in read-only mode for archival purposes. To learn how to continue using your existing account on the new forums, check out this thread.

Results 1 to 9 of 9

Thread: Custom Random function, please explain..

  1. #1

    Custom Random function, please explain..

    A friend of mine gave me this piece of code, and this way I can reset the random so it won't change when I redraw something using my interactive canvas (

    But I don't fully understand it...
    What he did tell me it has to do with:

    But that is waaaay above my comprehension level

    If any of you great Kirupian's can shed some light on this, I'd be really gratefull!!

    PHP Code:
            var m_wm_z;

    resetRandom = function() {
    m_w 17;
    m_z 23;

    unitRandom = function() {
    m_z 36969 * (m_z 65535) + (m_z >> 16);
    m_w 18000 * (m_w 65535) + (m_w >> 16);
               return ((
    m_w 65535) / 65536);
    random = function(ab){
    unitRandom() * (a) + a;
    As3 / JS/jQuery / HTML5 / CSS(3) / PHP (intermediate)

  2. #2
    That looks like a pretty cool piece and I might be the only one in saying I fail to see how that is randomising anything? I have done very little with bitwise operators and only understand the very basics. The comparisons he is doing here will result in the same value each time, the value of m_z and m_w would need to change in order to return a different result.

    Some more information to do your head in:

  3. #3
    Well yeah, that was what I was discovering yesterday/today also, I tried just creating this piece of code to try some stuff out, and just console.log everything. And I got the same value over and over and over again... So I don't really understand why the random is working in my outSide preset ( I'll dive into that link to maybe learn to create something myself.
    As3 / JS/jQuery / HTML5 / CSS(3) / PHP (intermediate)

  4. #4
    Ok I at least got a "random" value now, but like you said I can rerun this code all day long the "random" output is going to be the same because its based on those two values...
    But this is the only way (I can think of) to keep the scene from constantly changing (again check the outSide preset).
    Because the code is evaluated on every keystroke, when typing the mountains will constantly shift position(if you use a regular Math.random)...

    Blegh this is getting more and more complicated
    As3 / JS/jQuery / HTML5 / CSS(3) / PHP (intermediate)

  5. #5
    LCG is a method to generate pseudo random numbers. Math.random() does that for you. There are other PRNGs of which Blum Blum Shub (BBS) is my favourite.

  6. #6
    Please explain, you just used a set of abreviations that are total blabla to me
    As3 / JS/jQuery / HTML5 / CSS(3) / PHP (intermediate)

  7. #7
    What you are doing is generating a mountain. To get the rigged lines you use a customized random number generator.
    The value of unitRandom() changes each time in the drawMountain() while loop, because the value of m_z and m_w changes as a part of the previous computation. But when you look at the over all generated values, they are a constant. The generated value solely depends on the initial value you assign to m_z and m_w.
    Since m_z = 23 and m_w = 17 initially, no matter how many times you call, you'll generate the same pattern. So to change the pattern you need to change the value of m_z and m_w.
    I'm not very clear on how you got the formula in the unitRandom() function. It is a nice one. However to generate terrains you can use algorithms based on fractals.
    See: Generating random fractal terrains.

  8. #8
    Yeah! Nailed it. The algorithm is developed by George Marsaglia.

    Read his usenet posts:
    Random numbers for C: The END?
    Random numbers in C: Some suggestions

    You could also read on perlin noise algorithms.

  9. #9
    Wow thank you so much, that clears some stuff up
    As3 / JS/jQuery / HTML5 / CSS(3) / PHP (intermediate)

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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

Home About Meet the Moderators Advertise

 Link to Us


Copyright 1999 - 2012