Computer Science Canada

Random numbers

Author:  Neo [ Tue Nov 02, 2004 11:12 pm ]
Post subject:  Random numbers

Hello, I am very noobish in Java so I need some help. I read my notes an there is a function Math.random to generate random numbers between 0.0 and 1.0. I want to generate an integer between 1 and 6. I looked around on the internet and found this code
code:
int number=(int)(Math.random()*40)+10;
this generates a random number between 50 and 10. How exactly does this code work?

I also messed arounnd with it a bit and chagned it to
code:
int number=(int)(Math.random()*6)+1
to generate a random number between 1 and 6 and it seems to work, but I dont really understand why. Can someone please explain please. Thanks.

Author:  wtd [ Tue Nov 02, 2004 11:19 pm ]
Post subject: 

You want to look at the Java API docs for the Math class.

For the random method, go to:

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Math.html#random()

The Math.random() functions returns a double. This is a floating point number between 0 and 1.0.

To get that range to be 0 to 6, we simply multiply the result of it by 6, then convert it to an integer (disposing of everything after the decimal point. Of course, this will give us a number between 0 and 6, but it won't include 6. To do this, we simply add 1 and bump any random number generated up by 1.

Author:  Neo [ Wed Nov 03, 2004 2:21 pm ]
Post subject: 

wtd wrote:
You want to look at the Java API docs for the Math class.

For the random method, go to:

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Math.html#random()

The Math.random() functions returns a double. This is a floating point number between 0 and 1.0.

To get that range to be 0 to 6, we simply multiply the result of it by 6, then convert it to an integer (disposing of everything after the decimal point. Of course, this will give us a number between 0 and 6, but it won't include 6. To do this, we simply add 1 and bump any random number generated up by 1.


If we bump up every number by 1 would it not be impossible to get 0? and when we convert a double into an int is the number rounded up or down or no rounding at all? and last question so would that code i typed in generate a random number from 1 to 6?

Author:  wtd [ Wed Nov 03, 2004 2:58 pm ]
Post subject: 

Neo wrote:
wtd wrote:
You want to look at the Java API docs for the Math class.

For the random method, go to:

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Math.html#random()

The Math.random() functions returns a double. This is a floating point number between 0 and 1.0.

To get that range to be 0 to 6, we simply multiply the result of it by 6, then convert it to an integer (disposing of everything after the decimal point. Of course, this will give us a number between 0 and 6, but it won't include 6. To do this, we simply add 1 and bump any random number generated up by 1.


If we bump up every number by 1 would it not be impossible to get 0?


Yes. To get a number between 0 and 6, including 6, multiply by 7.

Neo wrote:
and when we convert a double into an int is the number rounded up or down or no rounding at all?


It simply discards everything after the decimal point.

Neo wrote:
and last question so would that code i typed in generate a random number from 1 to 6?


Yes.


: