I'm making a game where you have to collect coins but i need a way to make it spawn at a random location, can anyone help?


1 2 3 4 5 6 7 8 | // (1) int x = Greenfoot.getRandomNumber( 600 ); // (2) int x = ( int )(Math.random()* 600 ); // (3) int x = ( new java.util.Random()).nextInt( 600 ); |
1 2 3 4 | java.util.Random rand = new java.util.Random(); int x = rand.nextInt( 600 ); int y = rand.nextInt( 600 ); |
1 2 3 4 5 6 | Coin coin0 = new Coin(); addObject(coin0, Greenfoot.getRandomNumber( 300 ), Greenfoot.getRandomNumber( 200 ); Coin coin1 = new Coin(); addObject(coin1, Greenfoot.getRandomNumber( 300 ), Greenfoot.getRandomNumber( 200 ); Coin coin2 = new Coin(); addObject(coin2, Greenfoot.getRandomNumber( 300 ), Greenfoot.getRandomNumber( 200 ); |