This is a pretty simple Java (though probably applicable to all programming) question:
Math.random()
returns a number between zero and one.
If I want to return an integer between zero and hundred, I would do:
(int) Math.floor(Math.random() * 101)
Between one and hundred, I would do:
(int) Math.ceil(Math.random() * 100)
But what if I wanted to get a number between three and five? Will it be like following statement:
(int) Math.random() * 5 + 3
I know about nextInt()
in java.lang.util.Random
. But I want to learn how to do this with Math.random()
.