Generate Bank Account Number With Random

Generate bank account number with random

If you don't care about the validity of the number generated you can try something like this.

What it does is, I've created a String with initial value of BE, afterwards kept adding a random integer from [0,9] 14 times. This way now I have a string with the format you've wanted.

import java.util.Random;

public class HelloWorld
{
public static void main(String[] args)
{
Random rand = new Random();
String card = "BE";
for (int i = 0; i < 14; i++)
{
int n = rand.nextInt(10) + 0;
card += Integer.toString(n);
}
for (int i = 0; i < 16; i++)
{
if(i % 4 == 0)
System.out.print(" ");
System.out.print(card.charAt(i));
}
}
}

Example output(s):

BE69 5987 1676 6052
BE06 8159 1742 2133
BE36 6723 4210 0408
BE74 4759 6874 6751

Now I've updated the code with a simple demonstration of how to split the String each 4 characters. I'm not really familiar with Java, so pardon my mistakes.

Generate all unique bank account numbers

public static IEnumerable<int> BANS
{
get
{
int[] digits = { 1, 0, 0, 0, 0, 0, 0, 0, 2 };

int carryFlag = 0;
do
{
int sum = digits.Select((d, i) => d * (9 - i))
.Sum();

if (sum % 11 == 0)
yield return digits.Aggregate(0, (accumulator, digit) => accumulator * 10 + digit);

int digitIndex = digits.Length - 1;
do
{
digits[digitIndex] += 1;
if (digits[digitIndex] == 10)
{
digits[digitIndex--] = 0;
carryFlag = 1;
}
else
carryFlag = 0;
}
while (digitIndex >= 0 && carryFlag == 1);
}
while (carryFlag == 0);

yield break;
}
}

Console.WriteLine(BANS.Count()) gives 81818182 BAN's. The calculation time about 5 minutes in the virtual machine.

First 10 values are:

foreach (var ban in BANS.Take(10))
Console.WriteLine(ban);
----------------------------------
100000002
100000010
100000029
100000037
100000045
100000053
100000061
100000088
100000096
100000118

On the second question:

static void Main(string[] args)
{
const int MAX_FILES = 10;
const int BANS_PER_FILE = 10;

int filesCounter = 0;
int bansCounter = 0;
var part = new List<int>();
foreach (var ban in BANS)
{
part.Add(ban);

if (++bansCounter >= BANS_PER_FILE)
{
string fileName = string.Format("{0}-{1}.txt", part[0], part[part.Count - 1]);
Console.WriteLine("Filename '{0}'", fileName);
foreach (var partBan in part)
Console.WriteLine(partBan);

part.Clear();
bansCounter = 0;

if (++filesCounter >= MAX_FILES)
break;
}

}
}

How can I generate a 16 digit random number with rand() function in C?

There are several different issues with your program:

  1. The most noticeable is this line:

    cardnumber+= ( (10^i) * randd ) ;

    which does not do what you think it does. The ^ operator performs the bitwise XOR of the two operands, it does not compute a power.

  2. You are excluding 0 from your digits with a loop, that's unneeded (can be done without a loop) and also seems wrong, since 0 is a perfectly valid digit. If you want to force your number to have 16 digits then you only want the most significant one to be different than 0, not all of them.

  3. Your loop runs for i=0 to 14, so you are generating 15 digits, not 16.

  4. You are calling srand(time(0)) every time you call this function. This will make the function generate the same number if called within the same second (time(0) returns the current timestamp in seconds). You probably do not want this, and should call srand() only once in your main().

  5. There is no reason for your numbers to be signed, this can only cause potential problems, use unsigned.

Here's a reasonably simple version of the code which achieves what you want. If you are looking for a faster / more optimized version you might want to check out this other answer by chqrlie.

void generate_card_number()
{
unsigned long long cardnumber;
unsigned i;

// Ensure most significant digit is not 0
cardnumber = rand() % 9 + 1;

// Add 15 more digits (which can also be 0)
for (i = 0; i < 15; i++)
{
cardnumber *= 10;
cardnumber += rand() % 10;
}

printf("\nThe Card Number : %llu", cardnumber);
}

How to generate a sequential account number?

You could track the assigned account numbers statically, and assign the new account the next number in the series. Something like

public class Account
{

private static int nextAccoutNumber = 0;

private Customer cust;
private double balance;

Account(Customer c)
{
cust = c;
balance = 0;
accNum = ++nextAccountNumber;
}
}

You're adding the new account to the list, but you're also storing it in a local variable. Where you're doing this:

System.out.println(chaseAccts.get(i).getAccount() + " " + accCust.getAccNum())

you are writing out the accNum value of the same accCust each time. You need to write

System.out.println(chaseAccts.get(i).getAccount() + " " + chaseAccts.get(i).getAccNum());

And since you're using a list of type ArrayList<Account> you could just write the whole loop as:

public void display()
{
for(Account account : chaseAccts) {
System.out.println(account.getAccount() + " " + account.getAccNum());
}
}

How can I generate IBAN for Swedish Account number

The error you obtain gives a hint that you need to use full length of the account number as indicated in this example:

Iban iban = new Iban.Builder()
.countryCode(CountryCode.SE)
.bankCode("500")
.accountNumber("0000005839825746")
.build();

In other words, ensure that you are supplying expected 20 digits before calling .build().

Random Number Generator Algorithm

There are three general solutions to the non-duplicate random number problem:

  1. If you want a few numbers from a large range then pick one and reject it if it is a duplicate. If the range is large, then this won't cause too many repeated attempts. This is what you mention above.

  2. If you want a lot of numbers from a small range, then set out all the numbers in an array and shuffle the array. The Fisher-Yates algorithm is standard for array shuffling. Take the random numbers in sequence from the shuffled array.

  3. If you want a lot of numbers from a large range then use an appropriately sized encryption algorithm. E.g. for 64 bit numbers use DES and encrypt 0, 1, 2, 3, ... in sequence. The output is guaranteed unique because encryption is reversible. The Hasty Pudding Cipher can be set for any convenient range of numbers.



Related Topics



Leave a reply



Submit