Why use Math.random(), and what it does

Xahed Kamal

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?

Thomas C

From developer.mozilla.org

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Why does the MongoDB Java driver use a random number generator in a conditional?

From Dev

What does is mean by null pointing to nothing and why use null at all?

From Dev

Why does this not seem to be random?

From Dev

Concept of Math.floor(Math.random() * 5 + 1), what is the true range and why?

From Dev

What is the "&=" operator and why does Twilio use it when comparing strings?

From Dev

Why does my recursive function always use the same random numbers?

From Dev

What is the use of the Random(long) constructor?

From Dev

Why does software often use an inverse coordinate system compared to regular math coord system?

From Dev

Why does the next method in Random use a compareAndSet?

From Dev

Not being able to use Math.random properly

From Dev

What does Math.random() * i | 0 mean?

From Dev

In Java 8 why we cannot convert Math.random() to Math::random using method references

From Dev

What is this "Change to Display" of math equations and why does it change the equation style in Word 2010?

From Dev

Why does the Math.random() keep updating onClick?

From Dev

What does the operator/math sign ">>" mean in Javascript?

From Dev

Why does this not seem to be random?

From Dev

Concept of Math.floor(Math.random() * 5 + 1), what is the true range and why?

From Dev

math in java - what does " %" do?

From Dev

Why does Math.floor(Math.random()) function always return "0"?

From Dev

what is python and why does it use so much cpu periodically?

From Dev

What exactly does Math.floor do?

From Dev

What does Math.random() * i | 0 mean?

From Dev

Im making a program that makes random math, but it does not work

From Dev

Switch() does not work with Math.random() numbers

From Dev

what is utempter, and why does xterm want to use it?

From Dev

Why Math.random() type is number and Math.random is function?

From Dev

How does the distribution of Math.random()*50 + Math.random()*20 compare to Math.random()*70?

From Dev

Why JavaScript Math.random() returns same number multiple times

From Dev

Why does JavaScript have the Math object?

Related Related

  1. 1

    Why does the MongoDB Java driver use a random number generator in a conditional?

  2. 2

    What does is mean by null pointing to nothing and why use null at all?

  3. 3

    Why does this not seem to be random?

  4. 4

    Concept of Math.floor(Math.random() * 5 + 1), what is the true range and why?

  5. 5

    What is the "&=" operator and why does Twilio use it when comparing strings?

  6. 6

    Why does my recursive function always use the same random numbers?

  7. 7

    What is the use of the Random(long) constructor?

  8. 8

    Why does software often use an inverse coordinate system compared to regular math coord system?

  9. 9

    Why does the next method in Random use a compareAndSet?

  10. 10

    Not being able to use Math.random properly

  11. 11

    What does Math.random() * i | 0 mean?

  12. 12

    In Java 8 why we cannot convert Math.random() to Math::random using method references

  13. 13

    What is this "Change to Display" of math equations and why does it change the equation style in Word 2010?

  14. 14

    Why does the Math.random() keep updating onClick?

  15. 15

    What does the operator/math sign ">>" mean in Javascript?

  16. 16

    Why does this not seem to be random?

  17. 17

    Concept of Math.floor(Math.random() * 5 + 1), what is the true range and why?

  18. 18

    math in java - what does " %" do?

  19. 19

    Why does Math.floor(Math.random()) function always return "0"?

  20. 20

    what is python and why does it use so much cpu periodically?

  21. 21

    What exactly does Math.floor do?

  22. 22

    What does Math.random() * i | 0 mean?

  23. 23

    Im making a program that makes random math, but it does not work

  24. 24

    Switch() does not work with Math.random() numbers

  25. 25

    what is utempter, and why does xterm want to use it?

  26. 26

    Why Math.random() type is number and Math.random is function?

  27. 27

    How does the distribution of Math.random()*50 + Math.random()*20 compare to Math.random()*70?

  28. 28

    Why JavaScript Math.random() returns same number multiple times

  29. 29

    Why does JavaScript have the Math object?

HotTag

Archive