Ross Ross -4 years ago 140
Java Question

How to generate Random point(x,y) in Java

I want to generate a random point(x,y) in java which should lie within a 2D plane {(1,1)(1,-1)(-1,1)(-1,-1)} where both positive & negative X & Y axis is considered.Thank you

Answer Source

Take a look at article https://www.tutorialspoint.com/java/util/java_util_random.htm. All you need to do is to generate pairs of floats in range (-1,1). You should use method nextFloat() from class Random. It will give you numbers in range (0,1). Then multiply it by 2 and subtract 1 and you will have numbers in desired interval.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download