Reputation:
I need a function that will return a random integer that can be only -1, 0, or 1. Thanks?
Upvotes: 7
Views: 6510
Reputation: 22292
Random random = new Random();
int value = random.nextInt(3) - 1;
Upvotes: 5
Reputation: 134167
As Apocalisp wrote, you could do something like:
import java.util.Random;
Random generator = new Random();
int randomIndex = generator.nextInt( 3 ) - 1;
Upvotes: 18