-6

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

Shreyas Shetty
  • 27
  • 1
  • 2
  • 7

3 Answers3

2

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.

0

Please use SecureRandom see: StackOverflow

Community
  • 1
  • 1
d4u7
  • 29
  • 5
-1

Use this method below to return you the random points

public String randXYPoint(){
    //For x and y, the maximum point is 1 and Minimum point is -1
    //maximum number
    int min = -1;
    //minimum number
    int max = 1;

    // generates x values
    double xValue = min + Math.random() * (max - min);
    // generates y values
    double yValue = min + Math.random() * (max - min);
    //returns and converts points to string 
    return  String.valueOf(xValue)+ ", "+ String.valueOf(yValue);
}

Just call the method

System.out.println(randrandXYPoint())
Jjingo
  • 1
  • 1
  • 4