一个好的算法生成一个订单号 [英] A good algorithm for generating an order number

查看:1306
本文介绍了一个好的算法生成一个订单号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然我喜欢用的GUID在我的系统中的唯一标识符,它是不是很人性化像一个订单号码字段,其中一个客户可能有重复,为客户服务再presentative。

As much as I like using GUIDs as the unique identifiers in my system, it is not very user-friendly for fields like an order number where a customer may have to repeat that to a customer service representative.

什么是好的算法用于生成顺序编号,以便它是:

What's a good algorithm to use to generate order number so that it is:

  • 唯一
  • 在不连续的(纯粹是为了光学)
  • 只有
  • 在数字值(所以它可以很容易地阅读到CSR通过电话或键入)
  • < 10位
  • 可以在中间层产生没有做一个往返到数据库中。

更新(12/05/2009) 在仔细审查每一个发布的答案,我们决定将随机在中间层中的9位数字保存在数据库中。在发生碰撞的情况下,我们会重新生成一个新的号码。

UPDATE (12/05/2009) After carefully reviewing each of the answers posted, we decided to randomize a 9-digit number in the middle tier to be saved in the DB. In the case of a collision, we'll regenerate a new number.

推荐答案

如果中间层无法检查什么订单号已经存在于数据库中,最好也能做到将产生一个随机数的等价物。但是,如果您生成多数民众赞成限制为小于1十亿的随机数,你应该开始围绕担心意外碰撞时的sqrt(1十亿),也就是说,经过几数万条目生成这种方式,碰撞的危险材料。如果订单号是连续的,但在一个变相的方式,即一些大素数模在未来多个1十亿 - 将满足您的要求。

If the middle tier cannot check what "order numbers" already exists in the database, the best it can do will be the equivalent of generating a random number. However, if you generate a random number that's constrained to be less than 1 billion, you should start worrying about accidental collisions at around sqrt(1 billion), i.e., after a few tens of thousand entries generated this way, the risk of collisions is material. What if the order number is sequential but in a disguised way, i.e. the next multiple of some large prime number modulo 1 billion -- would that meet your requirements?

这篇关于一个好的算法生成一个订单号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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