Click here to Skip to main content
13,897,030 members
Rate this:
Please Sign up or sign in to 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;

Updated 22-Aug-18 2:21am
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
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
Patrice T 22-Aug-18 2:09am
I know it, but does the OP ? :)
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Here it is:
String startWith = "32";
Random generator = new Random();
String r = generator.Next(0, 999999).ToString("D6");
String aAccounNumber = startWith + r;
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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
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[^]

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.
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

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Cookies | Terms of Service
Web05 | 2.8.190306.1 | Last Updated 22 Aug 2018
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100