www.webdeveloper.com
Results 1 to 3 of 3

Thread: How Math.random()*6 +1|0 works?

  1. #1
    Join Date
    Nov 2013
    Posts
    2

    How Math.random()*6 +1|0 works?

    It looks like
    Math.random()*6 +1|0 works the same as
    Math.floor(Math.random()*6 + 1
    but I do not understand how 1|0 works.

  2. #2
    Join Date
    Mar 2007
    Location
    U.K.
    Posts
    1,127
    It is clearer when written
    Code:
    ( Math.random() * 6 + 1 ) | 0 ;
    A bitwise OR operation converts the result to an integer, removing the decimal fraction.
    Where used, return should be executed unconditionally and always as the last statement in the function.

    That's my signature, it's not part of the damn post!

  3. #3
    Join Date
    Nov 2013
    Posts
    2
    Thanks! That's cool!

Thread Information

Users Browsing this Thread

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

Tags for this Thread

Posting Permissions

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



Recent Articles