如何生成唯一的12位数字?

发布于 2024-08-25 04:56:18 字数 372 浏览 4 评论 0原文

我正在开发一个应用程序,它将原始数据发送到斑马打印机并打印出条形码。由于每个商品都有自己唯一的条形码,因此我需要定义一个变量来自动生成 12 位数字长的唯一编号。

请参阅示例:

printBar prnt = new printBar("123456789012");

是否有办法定义一个双精度变量并将其传递给返回唯一 12 位数字的函数,然后再次将其传递给 printBar 类?但是如何确保每次访问它都返回唯一的值呢?

我还想到了另一种方法,因为我使用MS Access db,我可以创建一列AutoNumber数据类型并将其分配给Random,但是你没有得到所需的确切12位数字,有时它会生成10位数字的值,有时更多或更少。

I'm working on an app that sends raw data to zebra printer and print out barcodes. And since every item has its own unique barcode, I need to define a variable that automatically generates unique number of 12 digits long.

see example:

printBar prnt = new printBar("123456789012");

Is there anyway to define a double variable and pass it to a function that return uniqely 12 digits number and pass it over again to the printBar class?. But how to make sure everytime you access it returns a unique value?.

I also thought of another way, since am using MS Access db, I can create a column of AutoNumber datatype and assign it to Random, but you don't get the exact 12 digits required, sometimes it generates a value of 10 digits sometimes more or less.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

如歌彻婉言 2024-09-01 04:56:18

从十二位数字开始,即:111111111111,

以获得新的“随机”唯一数字,取之前的数字并加 1。

虽然不是随机的,但它将保证唯一性。

Start with a twelve digit number, ie: 111111111111

to get your new 'random' unique number take the previous number and add 1.

although not random, it will guarantee uniqueness.

╭ゆ眷念 2024-09-01 04:56:18

您每天、每小时、每分钟生成多少次新条形码?

您可以使用 Visual Studio 的自动版本控制等技术。

  • 计算某个特定日期(例如 1.1.2000)的天数
    • 填充了 0 到 5 个位置。
  • Concat 到午夜为止所经过的秒数
    • 还填充了零到五个位置。
  • 在您的应用程序中用静态计数器填充最后两个数字,该计数器恰好在 99 处环绕。

示例

public static class UniqueId
{
    static private int _InternalCounter = 0;

    static public string Get()
    {
        var now = DateTime.Now;

        var days = (int)(now - new DateTime(2000, 1, 1)).TotalDays;
        var seconds = (int)(now - DateTime.Today).TotalSeconds;

        var counter = _InternalCounter++ % 100;

        return days.ToString("00000") + seconds.ToString("00000") + counter.ToString("00");
    }

通过这种方法,您将在 2273 年 10 月 15 日发生溢出,但我认为您的关注者可以解决这个问题。 ;-)

如果您需要每秒创建超过一百个唯一 ID,您可以将最后两行更改为:

var counter = _InternalCounter++ % 1000;
return days.ToString("0000") + seconds.ToString("00000") + counter.ToString("000");

现在您每秒将有一千个唯一 ID,但这些天将在 2027 年 5 月 18 日溢出。如果这是太短了,如果您通过以下行将开始日期设置为 2010 年,您可以获得额外的十年:

var days = (int)(now - new DateTime(2010, 1, 1)).TotalDays;

How many times do you generate a new barcode per day, hour, minute?

You could use a technique like the auto versioning of Visual Studio works.

  • Count the number of days from some specific date (e.g. 1.1.2000)
    • padded with 0 to five places.
  • Concat the seconds elapsed till midnight
    • padded also with zero to five places.
  • Fill up the last two numbers with a static counter in your App that just wrap around at 99.

Example

public static class UniqueId
{
    static private int _InternalCounter = 0;

    static public string Get()
    {
        var now = DateTime.Now;

        var days = (int)(now - new DateTime(2000, 1, 1)).TotalDays;
        var seconds = (int)(now - DateTime.Today).TotalSeconds;

        var counter = _InternalCounter++ % 100;

        return days.ToString("00000") + seconds.ToString("00000") + counter.ToString("00");
    }

With this approach you'll get an overflow at the 15. October 2273, but i think this can be solved by your follower. ;-)

If you need to create more than hundred unique IDs per second you can change the last two line into:

var counter = _InternalCounter++ % 1000;
return days.ToString("0000") + seconds.ToString("00000") + counter.ToString("000");

Now you'll have thousand unique IDs per second, but the days will already overflow at 18. May 2027. If this is too short, you can get additional ten years if you set the start date to 2010 by this line:

var days = (int)(now - new DateTime(2010, 1, 1)).TotalDays;
有深☉意 2024-09-01 04:56:18

使用 RNG 和哈希可以:

10 - 输出 12 位数字

20 - 检查值是否在哈希值中

30 - 如果是转到 40,否则转到 10

40 - 将值推入哈希值

50 - 返回新的 12 位数字

60 - 转到 10

Using an RNG and a hash do:

10 - stream out 12 digits

20 - check if value is in hash

30 - if it's goto 40 else goto 10

40 - push value into hash

50 - return new 12 digit number

60 - goto 10

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文