Results 1 to 9 of 9
-
March 25th, 2012, 01:52 PM #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 (www.mariusposthumus.nl/canvasFun)
But I don't fully understand it...
What he did tell me it has to do with: http://en.wikipedia.org/wiki/Linear_...tial_generator
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_w, m_z;
var resetRandom = function() {
m_w = 17;
m_z = 23;
};
var 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);
};
var random = function(a, b){
return unitRandom() * (b - a) + a;
};
As3 / JS/jQuery / HTML5 / CSS(3) / Java(learning) / PHP(learning)
Formerly known as: MJTheOne
-
March 25th, 2012, 07:37 PM #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:
https://developer.mozilla.org/en/Jav...wise_Operators
-
March 26th, 2012, 04:47 AM #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 (www.mariusposthumus.nl/canvasFun).. I'll dive into that link to maybe learn to create something myself.
Thx!As3 / JS/jQuery / HTML5 / CSS(3) / Java(learning) / PHP(learning)
Formerly known as: MJTheOne
-
March 26th, 2012, 05:48 AM #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) / Java(learning) / PHP(learning)
Formerly known as: MJTheOne
-
March 29th, 2012, 06:57 AM #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.
-
March 29th, 2012, 07:48 AM #6
Please explain, you just used a set of abreviations that are total blabla to me
As3 / JS/jQuery / HTML5 / CSS(3) / Java(learning) / PHP(learning)
Formerly known as: MJTheOne
-
March 30th, 2012, 05:51 AM #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.
-
March 30th, 2012, 06:20 AM #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.
-
March 30th, 2012, 06:48 AM #9
Wow thank you so much, that clears some stuff up
As3 / JS/jQuery / HTML5 / CSS(3) / Java(learning) / PHP(learning)
Formerly known as: MJTheOne

Reply With Quote

Bookmarks