Here I have a piece of code:
if (Math.random() < 0.80) {
var img = $('#img');
}
$(document).mousemove(function(event) {
var mouse_x = event.pageX;
var mouse_y = event.pageY;
$(img).css({
'top': mouse_y+'px',
'left': mouse_x+'px',
'display' : 'block',
'position' : 'absolute'
});
});
In this script, I don't understand what the if (Math.random() < 0.80)
line is doing. And how does Math.random()
gets its value, from where?
The Math.random() function returns a floating-point, pseudo-random number in the range [0, 1) that is, from 0 (inclusive) up to but not including 1 (exclusive), which you can then scale to your desired range. The implementation selects the initial seed to the random number generation algorithm; it cannot be chosen or reset by the user.
In your code Math.random() generates a psudeo random number < 1 and then if that number is less than 0.80 the code inside the if block executes.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments