[Java] random number from 1 - 50?

waaaks!

Zinctified
Reaction score
256
what function should i use to get a random number from 1 to 50?
im learning java and i cant find any function to get a random number within a given range

all i can find is random() and rint(), the worst part is, i dont know how to use them :)

thanks in advance
 

enouwee

Non ex transverso sed deorsum
Reaction score
240
You may want to use something better for larger numbers though.

Code:
import java.util.Random;

class Test
{
	private static int getRandomNumber(int max)
	{
		Random ran = new Random();
		return 1 + ran.nextInt(max);
	}

	public static void main(String args[])
	{
		System.out.println(getRandomNumber(50));
	}
}
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top