Get the random number according to the cardinal number. - Android java.util

Android examples for java.util:Random

Description

Get the random number according to the cardinal number.

Demo Code


//package com.java2s;
import java.util.Random;

public class Main {
    /**//from w  ww.  j a  v a2 s . c  o  m
     * Get the random number according to the cardinal number.
     * 
     * @param  cardinal the cardinal number, the max random number will be (cardinal-1)
     * @return          the random number
     */
    public static int random(int cardinal) {
        Random random = new Random();
        return cardinal == 0 ? 0 : Math.abs(random.nextInt()) % cardinal;
    }
}

Related Tutorials