+ 14
a , b //where a>b int x=(new Random()).nextInt(a-b+1)+b; //note ::: considering between includes extreme values also â˜ș
31st Dec 2017, 7:43 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 1
import java.util.Random; public class Program { public static void main(String[] args) { Random r = new Random(); int x = 2 + r.nextInt(2); System.out.println(x); } } This is how! 2 + nextInt(2)... will choose between 0 or 1 randomly (i.e., max 2 numbers starting from 0). Then you're adding 2 to whatever number system chooses. If it chooses 0 becomes 2, and if 1 then it'll become 3. Thus results in 2 or 3 as output.
31st Dec 2017, 7:18 PM
Harjeet Singh
Harjeet Singh - avatar