Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to generate bank account number, the number must start with 32 like for example 32098723, 32162545, 32752934 etc.. and it has to be of be 8 digits. Can anyone help me please and thank you.

What I have tried:

public long GetNewAccountNumber(long accountNumber)
{
Random random = new Random();
long newAccountNumber = random.Next(32000000, 33000000);


return newAccountNumber;




}
Posted
Updated 22-Aug-18 2:21am
Comments
Patrice T 21-Aug-18 19:58pm    
Do you have a question or a problem ?
Ron23111 21-Aug-18 20:13pm    
this is my question that did I do it right or not
Patrice T 21-Aug-18 20:44pm    
Depend.
What is the difference between a 8 digit random number and a bank account ?
George Swan 22-Aug-18 1:29am    
Bank account numbers are not random. Some sort of modulus arithmetic is applied to them. See this https://www.vocalink.com/media/1733/vocalink-validating-account-numbers-v350.pdf
Patrice T 22-Aug-18 2:09am    
I know it, but does the OP ? :)

Here it is:
String startWith = "32";
Random generator = new Random();
String r = generator.Next(0, 999999).ToString("D6");
String aAccounNumber = startWith + r;
 
Share this answer
 
v2
Solution 1 is providing you with pseudo-random numbers and should really have warned you about the implications of that (e.g. you could end up with duplicates). See the documentation for further information Random Class (System) | Microsoft Docs[^]

You would probably be better off using the RNGCrypto​Service​Provider Class[^]. There are examples of how to do that on that link.

Once you have your random number r you can generate your bank account number with
C#
string accNumber = String.Format("32{0}", r.ToString("D6"));
Now note the comments from @ppolymorphe and @George-Swan - Bank Accounts are not just random numbers, they follow very specific rules. Once you have generated your number you should check that it is valid.

Here is a clickable version of George's link https://www.vocalink.com/media/1733/vocalink-validating-account-numbers-v350.pdf[^]

Here is another example of a validation tool Sortcode and Bank Account Validation - C# Developers Guide[^]

And here is a Codeproject article on the subject IBAN Validator[^]

There are many commerical products and APIs available to do the validation.
 
Share this answer
 
Comments
Ron23111 22-Aug-18 21:13pm    
Thank you so much for your help CHill60 and Er. Puneet Goel
Er. Puneet Goel 27-Aug-18 1:05am    
Welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900