title: Math.random and decimals
body: <p>I really want to understand, thx for help.
  I've been watching Derek Banas Java Programming video and I got stuck to this:</p>
  
  <p>int randomNumber = (int) (Math.random() * 126) + 1;</p>
  
  <p>The goal is to get a random number from 1 to 126 included.
  It works but why? How can it output 126?</p>
  
  <ul>
  <li><p>If casting int drop decimals.</p>
  
  <blockquote>
    <p>126included125(See testing)</p>
  </blockquote></li>
  <li><p>If Math.random() returns 0 - 0.9 inclusive.</p></li>
  </ul>
  
  <p>The nearest would be like:</p>
  
  <ol>
  <li>int randomNumber = (int) (Math.random() * 126) + 1;</li>
  <li>int randomNumber = (int) (0.99 * 126) + 1;</li>
  <li>int randomNumber = (int) (124.74) + 1;</li>
  <li>int randomNumber = (int) 125.74;</li>
  <li>int randomNumber = 125;</li>
  </ol>
  
  <p>Can someone explain to me why Im wrong.</p>
  
  <p>Testing:</p>
  
  <pre><code>import java.util.*;
  
  public class Random_Number_Test 
  {
      public static void main(String[] args) 
      {
          boolean Program_On_Going = true;
          double RoundTest = 125.74;
  
          while(Program_On_Going)
          {
              int randomNumber = (int) (Math.random() * 126) + 1;
  
              if(randomNumber == 126)
              {
                  System.out.print(randomNumber);
                  System.out.print("included");
                  Program_On_Going = false;
  
                  System.out.print((int) RoundTest);
              }
              else
              {
                  System.out.println(randomNumber);
              }
          }
      }
  }
  </code></pre>
  
  <p>Results</p>
  
  <blockquote>
    <p>18
    30
    101
    77
    101
    85
    87
    6
    89
    26
    20
    115
    108
    13
    53
    30
    85
    108
    41
    110
    22
    3
    38
    106
    46
    102
    117
    95
    36
    44
    65
    87
    87
    4
    114
    70
    8
    120
    50
    28
    60
    97
    101
    25
    30
    96
    4
    105
    55
    29
    20
    77
    105
    91
    19
    81
    105
    66
    1
    78
    81
    48
    77
    82
    11
    68
    113
    1
    7
    90
    104
    96
    100
    70
    114
    14
    125
    42
    108
    16
    119
    103
    70
    84
    83
    42
    15
    102
    45
    69
    80
    123
    56
    39
    115
    88
    70
    111
    30
    68
    19
    34
    17
    24
    71
    107
    81
    76
    42
    80
    123
    19
    54
    47
    96
    58
    64
    52
    61
    117
    64
    103
    47
    40
    60
    35
    23
    90
    75
    91
    29
    16
    7
    15
    20
    50
    85
    72
    109
    85
    108
    47
    54
    79
    91
    102
    84
    101
    5
    17
    50
    115
    115
    71
    67
    83
    15
    99
    77
    101
    20
    79
    22
    25
    30
    108
    46
    75
    89
    80
    103
    51
    61
    15
    91
    9
    61
    90
    11
    5
    27
    36
    116
    15
    14
    105
    5
    95
    53
    97
    55
    59
    1
    81
    97
    109
    77
    37
    3
    84
    115
    87
    15
    40
    93
    61
    68
    14
    93
    15
    38
    55
    15
    72
    48
    103
    75
    42
    63
    64
    61
    114
    103
    17
    47
    31
    102
    56
    57
    113
    82
    85
    48
    1
    120
    117
    25
    77
    101
    19
    84
    54
    101
    102
    2
    14
    114
    105
    20
    57
    70
    94
    7
    27
    123
    108
    109
    47
    4
    28
    72
    49
    86
    84
    64
    86
    71
    69
    33
    110
    109
    19
    71
    92
    49
    50
    77
    29
    31
    55
    62
    75
    61
    8
    52
    115
    69
    39
    71
    107
    42
    57
    92
    88
    56
    88
    89
    7
    123
    49
    13
    107
    6
    99
    21
    29
    49
    3
    82
    60
    104
    70
    123
    91
    4
    14
    57
    63
    27
    126included125</p>
  </blockquote>