title: How does Math.random() EXACTLY work in Java?
body: <p>I recently came across the </p>
  
  <blockquote>
    <p>Math.random( ) </p>
  </blockquote>
  
  <p>class in Java. (And) I was wondering about the class. For example, I'm trying to write a dice game that needs random numbers from 1 to 6 each "roll". This is where math.random( ) comes in. This line of code: </p>
  
  <pre><code>int random_num = (int) (Math.random() * 6) + 1;
  </code></pre>
  
  <p>CAN generate random numbers between 1 to 6. </p>
  
  <p>My problem lies at</p>
  
  <pre><code>(Math.random() * 6)
  </code></pre>
  
  <p>here.</p>
  
  <p>I know how this code works, how Math.random() generates a double value between 0.0 to 1.0. And how it multiplied by 6, and was rounded in the end.</p>
  
  <p>I was however, puzzled why a random number (say 0.78396954122) multiplied by 6 could become a random number between 1 to 6. Supposedly the random number  0.78396954122  multiplied by 6, is always 6, right? There's no way a  2.78396954122  can suddenly pop up!</p>
  
  <p>I'm a complete noob at Java and would appreciate if you could help explain this.</p>
  
  <p>Thanks...in advance! </p>