开发者

Generate a unique number without database

How do I generate a unique number in c# without the database?

(max 17 di开发者_如何学Gogits).

EDIT: digits only.


You could take a look at Twitter's open source Snowflake project for inspiration, which solves a similar problem. It's written in Scala, but a simplified version is quite simple to code in C#.

The premise is to get the current timestamp, and bitwise shift it left, leaving the right-hand bits available for a simple sequence number, which is reset to 0 every time the timestamp increments.

By playing around with the bit lengths, and altering the granularity of your timestamp (e.g. decide whether to use seconds or milliseconds), you should be able to produce something which can fit into 17 digits quite simply.


You can use GUID, A GUID (Globally unique identifier) is a 128-bit integer that can be used to uniquely identify something.

System.Guid.NewGuid()

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜