Java Generate Random number {-1,0,1}

Tags: java random
By : Zbigh1
Source: Stackoverflow.com
Question!

I need a function that will return a random integer that can be only -1, 0, or 1. Thanks?

By : Zbigh1


Answers

This should help:

Random random = new Random();
int value = random.nextInt(3) - 1;
By : Bob Cross


As Apocalisp wrote, you could do something like:

import java.util.Random;

Random generator = new Random();
int randomIndex = generator.nextInt( 3 ) - 1;


How about generating a random from 0 to 2 and subtracting 1?

By : Apocalisp


This video can help you solving your question :)
By: admin