Possible Duplicate:
Generating random number in a range with Java
double x = //Random number between -0.5 and 0.5
Possible Outputs:
-0.23
0.01
0.26
-0.4
How do I generate a double between the range of (example) -0.5 and 0.5?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
This should do it
Math.randomwill generate betweeen0and1. If you want between-0.5and+0.5then you can just-0.5from this result. See the API docsOne thing that this will not do is ever give you
0.5asMath.random()does never return1. This post will give you more details and a possible solution.