Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- //You need java.util.Random in otder to general random numbers
- public static int randInt(int min, int max)
- {
- //First, you create a new Random object
- Random rand = new Random();
- //Then, you get the next random integer; the range normally is from 0 to max, exclusive, so you add one
- //After getting a random number, you add the minimum, so you stay within the range
- //For example, rand.nextInt(3) might return 0 or 1 or 2, and you want something between 3 and 6; the smallest (0)
- // should then have 3 added to it to stay in the range.
- int randomNum = rand.nextInt((max - min) + 1) + min;
- // Then you can return it
- return randomNum;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement