
-----------------------------------
Neo
Tue Nov 02, 2004 11:12 pm

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 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 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.

-----------------------------------
wtd
Tue Nov 02, 2004 11:19 pm


-----------------------------------
You want to look at the Java API docs for the 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.

-----------------------------------
Neo
Wed Nov 03, 2004 2:21 pm


-----------------------------------
You want to look at the Java API docs for the 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?

-----------------------------------
wtd
Wed Nov 03, 2004 2:58 pm


-----------------------------------
You want to look at the Java API docs for the 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.

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.

and last question so would that code i typed in generate a random number from 1 to 6?

Yes.
