Saturday, 19 July 2014

Generate random numbers in between min and max

import java.util.*;

public class GetRandom {
    
    public static int getRandom(int min, int max){
        Random rand = new Random();
        int randomNum = rand.nextInt((max - min) + 1) + min;
        return randomNum;
    }
    
    public static void main(String args[]){
        System.out.println(getRandom(5, 45));
        System.out.println(getRandom(15, 45));
        System.out.println(getRandom(45, 50));
    }
}

Sample Output
18
29
45



                                                             Home

No comments:

Post a Comment