//package br.com.tecnove.random.android.util;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Random;
public class RandomGenerator {
private static final Random r = new Random();
/**
* Random a value from a specified range
*
* @param min
* minimun range (inclusive)
* @param max
* maximun range (inclusive)
* @return randomed int value
*/
public static int generateFromRange(int min, int max) {
// swap values if they come in the wrong order
if (min > max) {
int aux = min;
min = max;
max = aux;
}
int i = max - min;
int x = r.nextInt(i + 1);
return x + min;
}
/**
* Random a value from a string vector
*
* @param input
* String[] containing all the possible values
* @return randomed String value
*/
public static String generateFromStringArray(String[] input) {
int i = generateFromRange(0, input.length - 1);
return input[i];
}
/**
* Random a value from a string collection. Just a facilitator for the generateFromStringArray
*
* @param input
* Collection of String that contains the possible values
* @return randomed String value
*/
public static String generateFromStringCollection(Collection input) {
return generateFromStringArray((String[]) input.toArray());
}
/**
* Generate a random upercase char
*
* @return randomed char
*/
public static char generateLetter() {
return (char) generateFromRange((int) 'A', (int) 'Z');
}
/**
* Generate random numbers for a lottery
* @param maxRange limit the range from 1 to this number (included)
* @param numbersToGenerate how many numbers will be generated
* @return List of Integers with the non repeating values
*/
public static List generateLottery(Integer maxRange, Integer numbersToGenerate) {
List numbersGenerated = new ArrayList();
for (int c = 0; c < numbersToGenerate; c++) {
int nGenerated = 0;
do {
nGenerated = r.nextInt(maxRange);
// adding +1 to make the int start at 1 and the maxRange be included in the sorted values
nGenerated++;
// if the number is allready on the list, sort a new number
} while (numbersGenerated.contains(nGenerated));
numbersGenerated.add(nGenerated);
}
return numbersGenerated;
}
/**
* Random a value from a string with values separated by '/'
* @param valor
* @return
*/
public static String generateFromString(String valor) {
// the input must be like: curitiba/fortaleza/google/facebook/android/motorola
String[] vetor = valor.split("/");
String outValue = "";
if (vetor.length>0){
outValue = vetor[r.nextInt(vetor.length)];
}
return outValue;
}
}