Will Dev

Ranch Hand

Posts: 31

posted 1 year ago

Hi everybody

Could someone please help me understand the mathematical function (Math.random()* 10).

I understand it is a mathematical function that generates random numbers, but how does *10 limit the random numbers from 0 to 9. is it just something i must remember or is there a reason behind it.

Could someone please help me understand the mathematical function (Math.random()* 10).

I understand it is a mathematical function that generates random numbers, but how does *10 limit the random numbers from 0 to 9. is it just something i must remember or is there a reason behind it.

Norm Radder

Ranch Foreman

Posts: 2240

28

Campbell Ritchie

Marshal

Posts: 55793

164

posted 1 year ago

- 1

Don't do arithmetic on Math#random. Create an instance of the Random class because it has many methods which are easier to use. Read their documentation carefully before you use them. You probably want myRandom.nextInt(10).

Note that Math#random uses a Random instance behind the scenes anyway.

Note that Math#random uses a Random instance behind the scenes anyway.

Consider Paul's rocket mass heater. |