import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
public class MyRand {
private ArrayList<Integer> ids;
private Integer r_count;
private static DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
private static long INITIAL_DATE_MILLIS;
private static long FINAL_DATE_MILLIS;
static {
try {
//INITIAL_DATE_MILLIS = formatter.parse("1900-01-01").getTime(); //112 years before today.
INITIAL_DATE_MILLIS = formatter.parse("1995-01-01").getTime(); //112 years before today.
FINAL_DATE_MILLIS = (new Date()).getTime();
}
catch (ParseException e) {
e.printStackTrace();
}
}
private static final long MILLIS_PER_DAY = 24 * 60 * 60 * 1000;
private static final long DIAS_COUNT = (FINAL_DATE_MILLIS - INITIAL_DATE_MILLIS) / MILLIS_PER_DAY;
public MyRand(Integer count){
this.r_count = count;
this.ids = new ArrayList<Integer>();
this.reSetRandomInt();
}
/**
* @param args
*/
public static void main(String[] args) {
System.out.println( MyRand.RandomString(20) );
System.out.println( MyRand.RandomString(20) );
System.out.println( MyRand.RandomString(10) );
System.out.println();
System.out.println();
System.out.println( MyRand.RandomDate() );
System.out.println( MyRand.RandomDate() );
System.out.println( MyRand.RandomDate() );
System.out.println( MyRand.RandomDate() );
System.out.println( MyRand.RandomDate() );
System.out.println();
System.out.println();
for(int i = 0; i < 100; i++){
System.out.println( MyRand.RandomInt(1, 55));
}
for(int i = 0; i < 5; i++){
System.out.println( MyRand.RandomDate("2012-10-01", "2012-10-02") );
}
for(int i = 0; i < 5; i++){
System.out.println( MyRand.MasKedNumber(null) );
}
for(int i = 0; i < 10000; i++){
System.out.print( (MyRand.RandomInt(1, 100) == 10)+ ";\n ");
//System.out.println(MyRand.RandomUF());
/*
if(MyRand.RandomInt(0, 10) == 9){
System.out.println("xzz");
}
*/
}
}
public static String RandomDate(){
Date antes = new Date(INITIAL_DATE_MILLIS + MILLIS_PER_DAY * MyRand.RandomInt(1, (int)DIAS_COUNT) );
return formatter.format( antes );
}
public static String RandomUF(){
String[] estados = {"AC","AL","AP","AM","BA","CE","DF","ES","GO","MA","MT","MS","MG","PA","PB","PR","PE","PI","RJ","RN","RS","RO","RR","SC","SP","SE","TO"};
Integer i = RandomInt(0,estados.length);
return estados[i];
}
public void reSetRandomInt(){
for(int i = 0; i < this.r_count; i++){
ids.add(i+1);
}
}
public Integer RandomId(){
Integer id = ids.get(RandomInt(0,ids.size()-1));
ids.remove(id);
if(ids.size() == 0){
this.reSetRandomInt();
}
return id;
}
public static String RandomDate(String startDate, String endDate){
try {
long start_date = formatter.parse(startDate).getTime();
long end_date = formatter.parse(endDate).getTime();
long days = (end_date - start_date) / MILLIS_PER_DAY;
Date data = new Date( start_date + MILLIS_PER_DAY * MyRand.RandomInt(0, (int)days + 1 ) );
return formatter.format( data );
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
/*
* @param i integer from 0 to 60
* */
private static char getCustomChar(int i){
//Space
char c = ' ';
if(i < 26){
//Upper Case
c = (char) (i + 65);
}
else if(i < 52){
//Lower Case; 25 <= x <= 50
c = (char) (i + 71);
}
else if (i < 61)
{
// Numbers
c = (char) (i - 3);
}
return c;
}
/**
* Cria um string randomizado do tamanho que for solicitado.
* @param size vai ser o tamanho do campo na tabela
*/
public static String RandomString(int size){
char[] c = new char[size];
for(int i = 0; i < size; i++){
c[i] = MyRand.getCustomChar( MyRand.RandomInt(0, 62) );
}
return new String(c);
}
/**
* @param start limite inferior do numero
* @param size amplitude do numero
*/
public static Integer RandomInt(int start, int end){
return (int)(start + Math.random() * end);
}
public static String RandomNumber(int size){
String number = "";
for(int i = 0; i < size; i++){
number += MyRand.RandomInt(0,9).toString();
}
return number;
}
public static String MasKedNumber(String mask){
if(mask == null){
mask = new String("+(55) ?? ???? - ????"); //16 characters
}
char[] t = mask.toCharArray();
mask = "";
for(int i = 0; i < t.length; i++){
if(t[i] == '?'){ t[i] = MyRand.RandomInt(0, 10).toString().charAt(0); }
mask += t[i];
}
return mask;
}
public static String Randomize(MyColumn columnType){
if( columnType.getType().toLowerCase().equals("int") ){
return MyRand.RandomInt(0, 100000).toString();
}
else if( columnType.getType().toLowerCase().equals("date") ){
return MyRand.RandomDate();
}
else if( columnType.getType().toLowerCase().equals("numeric") ){
return MyRand.RandomNumber(columnType.getSize());
}
else if( columnType.getType().toLowerCase().equals("mask") ){
return MyRand.MasKedNumber(columnType.getMask());
}
else{
return MyRand.RandomString(columnType.getSize());
}
}
}