Returns a random number between 0 (inclusive) and the specified value (inclusive). - Android java.util

Android examples for java.util:Random

Description

Returns a random number between 0 (inclusive) and the specified value (inclusive).

Demo Code


//package com.java2s;

public class Main {
    static private int randomSeed = (int) System.currentTimeMillis();

    /**/*from  w  w w . ja  va2 s.c  o  m*/
     * Returns a random number between 0 (inclusive) and the specified value (inclusive).
     *
     * @param range Must be >= 0.
     */
    static public final int random(int range) {
        int seed = randomSeed * 1103515245 + 12345;
        randomSeed = seed;
        return ((seed >>> 15) * (range + 1)) >>> 17;
    }

    static public final int random(int start, int end) {
        int seed = randomSeed * 1103515245 + 12345;
        randomSeed = seed;
        return (((seed >>> 15) * ((end - start) + 1)) >>> 17) + start;
    }

    static public final float random() {
        int seed = randomSeed * 1103515245 + 12345;
        randomSeed = seed;
        return (seed >>> 8) * (1f / (1 << 24));
    }
}

Related Tutorials