Randomizer.java Source code

Java tutorial

Introduction

Here is the source code for Randomizer.java

Source

//package gameEngine.logic.utils;

import java.util.Random;

public class Randomizer {
    static {
        m_Random = new Random();
    }

    public static void generatePermutationOnArray(Object[] array) {
        Object tmp = null;
        int currIndex = 0;
        for (int i = array.length; i > 0; i--) {
            currIndex = m_Random.nextInt(i);
            tmp = array[currIndex];
            array[currIndex] = array[i - 1];
            array[i - 1] = tmp;
        }
    }

    public static int getRandomNumber(int lowerBound, int higherBound) {
        int delta = higherBound - lowerBound + 1;
        return (m_Random.nextInt(delta) + lowerBound);
    }

    private static Random m_Random;

    public static void main(String args[]) {
        Integer arr[] = new Integer[20];
        for (int i = 0; i < 20; i++) {
            arr[i] = new Integer(i);
        }
        for (int i = 0; i < 5; i++) {
            System.out.println("");
            generatePermutationOnArray(arr);

        }
    }

    public static double getRandomDouble() {
        double rand = m_Random.nextDouble();
        boolean flag = m_Random.nextBoolean();
        if (flag == true) {
            rand *= -1.0;
        }
        return rand;
    }
}