如何在C#上生成随机银行帐号 [英] How do I generate random bank account number on C#

查看:188
本文介绍了如何在C#上生成随机银行帐号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想生成银行帐号,该号码必须以32开头,例如32098723,32162545,32752934等。它必须是8位数。任何人都可以帮助我,谢谢你。



我尝试过:



public long GetNewAccountNumber(long accountNumber)

{

随机随机= new Random();

long newAccountNumber = random.Next( 32000000,33000000);





返回newAccountNumber;









}

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;




}

推荐答案

这是:

Here it is:
String startWith = "32";
Random generator = new Random();
String r = generator.Next(0, 999999).ToString("D6");
String aAccounNumber = startWith + r;


解决方案1为您提供伪随机数,并且应该真的警告过您的含义(例如,您最终可能会一式两份)。有关详细信息,请参阅文档随机类(系统)| Microsoft Docs [ ^ ]



您可能最好使用 RNGCrypto服务提供商类 [ ^ ]。有一些如何在该链接上执行该操作的示例。



一旦你有随机数 r 你就可以使用
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"));

现在注意来自@ppolymorphe和@ George-Swan的评论 - 银行账户不仅仅是随机数,他们遵循非常具体的规则。一旦你产生了你的号码,你应该检查它是否有效。



这是George的链接的可点击版本 https://www.vocalink.com/media/1733/vocalink-validating-account-numbers-v350.pdf [ ^ ]



这是验证工具的另一个例子排序码和银行帐户验证 - C#开发人员指南 [ ^ ]



这是一篇关于主题的Codeproject文章IBAN Validator [ ^ ]



有许多商业产品和API可以进行验证。

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.


这篇关于如何在C#上生成随机银行帐号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆