Sie sind auf Seite 1von 2

package br.jus.tjrr.servidor.presentation.

faces;
import java.security.cert.CollectionCertStoreParameters;
import java.util.*;
public class RandomUtil {
static String strrev(String str) {
if (str == null)
return "";
String revstr = "";
for (int i = str.length() - 1; i >= 0; i--) {
revstr += str.charAt(i);
}
return revstr;
}
/*
* 'prefix' is the start of the CC number as a string, any number of digits.
* 'length' is the length of the CC number to generate. Typically 13 or 16
*/
static String completed_number(int length) {
String ccnumber = "";
// generate digits
while (ccnumber.length() < (length - 1)) {
ccnumber += new Double(Math.floor(Math.random() * 10)).intValue();
}
// reverse number and convert to int
String reversedCCnumberString = strrev(ccnumber);
List<Integer> reversedCCnumberList = new Vector<Integer>();
for (int i = 0; i < reversedCCnumberString.length(); i++) {
reversedCCnumberList.add(new Integer(String
.valueOf(reversedCCnumberString.charAt(i))));
}
// calculate sum
int sum = 0;
int pos = 0;
Integer[] reversedCCnumber = reversedCCnumberList
.toArray(new Integer[reversedCCnumberList.size()]);
while (pos < length - 1) {
int odd = reversedCCnumber[pos] * 2;
if (odd > 9) {
odd -= 9;
}
sum += odd;
if (pos != (length - 2)) {

sum += reversedCCnumber[pos + 1];


}
pos += 2;
}
// calculate check digit
int checkdigit = new Double(
((Math.floor(sum / 10) + 1) * 10 - sum) % 10).intValue();
ccnumber += checkdigit;
return ccnumber;
}
public static String[] credit_card_number(int length, int howMany) {
Stack<String> result = new Stack<String>();
for (int i = 0; i < howMany; i++) {
result.push(completed_number(length));
}
return result.toArray(new String[result.size()]);
}
public static String[] generateCardNumbers(int howMany) {
return credit_card_number(16, howMany);
}
public static void main(String[] args) {
int numberOfCards = 500000;
String[] creditcardnumbers = generateCardNumbers(numberOfCards);
List<String> stringList = new ArrayList<String>(Arrays.asList(creditcard
numbers));
Set<String> stringSet = new HashSet<>(Arrays.asList(creditcardnumbers));
for (int i = 0; i < creditcardnumbers.length; i++) {
System.out.println(creditcardnumbers[i]);
}
System.out.println("List: " + stringList.size());
System.out.println("Set: " + stringSet.size());
}
}

Das könnte Ihnen auch gefallen