Expression with Math.random() always returns the same value
问题 For some reason this line of code int u=(int)Math.random()*6 + 1; will only return a 1 as a result. I found out that its just skipping the whole (int)Math.random()*6 and is only using the 1 as when I changed it to 2 it only returned 2. Anyone know What's happening? 回答1: The cast of Math.random() to int is occurring before the multiplication by 6. The cast operator is of higher precedence than * . The Math.random() method returns a random number between 0 (inclusive) and 1 (exclusive), so the