Back

So, the first answer is to use the java.util.Random class:

import java.util.Random;Random rand = new Random();// Acquire a number from [0 - 49].int n = rand.nextInt(50);// Append 1 to the result to get a number from the required range// (i.e., [1 - 50]).n += 1;

import java.util.Random;

Random rand = new Random();

// Acquire a number from [0 - 49].

int n = rand.nextInt(50);

// Append 1 to the result to get a number from the required range

// (i.e., [1 - 50]).

n += 1;

Another answer is using Math.random():

double random = Math.random() * 49 + 1;

or you can try

int random = (int)(Math.random() * 50 + 1);

31k questions

32.8k answers

501 comments

693 users