We are about to switch to a new forum software. Until then we have removed the registration on this forum.
writing a little program that displaces the coordinates of little circles but it displays them as numbers with many decimals. is there a way to use the random function with ints, or is there a way to display whole numbers
code:
final int DIAM = 5;
String coordinate;
void setup(){ size(500,500); frameRate(1);
}
void draw(){
float randomX = random(80,420); float randomY = random(20,495);
coordinate = "(" + randomX + "," + randomY + ")";
fill(255);
ellipse(randomX,randomY,DIAM,DIAM);
textSize(16); fill(0); text( coordinate,randomX + DIAM,randomY - DIAM);
}
Answers
float randomX = random(80, 420), randomY = random(20, 495);
int randomX = (int) random(80, 420), randomY = (int) random(20, 495);
Note that this gives you a random integer between a and b-1, it'll never return b.
The answer above is the easiest way: generate a number, then convert it to an int ( which rounds it in the process).
If you don't use the Processing built-in random, there are also several other ways to generate random integers, like Java.utils.Random.nextInt():
Random numbers can be generated using math.random() method. Here's the example for random number in java.