Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Random numbers
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Neo




PostPosted: 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.
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Tue Nov 02, 2004 11:19 pm   Post subject: (No 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.
Neo




PostPosted: Wed Nov 03, 2004 2:21 pm   Post subject: (No 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?
wtd




PostPosted: Wed Nov 03, 2004 2:58 pm   Post subject: (No 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.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: