How to generate a non-repeating random number

Source: Internet
Author: User
There are at least the following requirements

  1. is hashed and random, at least not guessed sequence rules
  2. is never repeated, if the build once to verify the existence, that efficiency is not too low?

Reply content:

There are at least the following requirements

  1. is hashed and random, at least not guessed sequence rules
  2. is never repeated, if the build once to verify the existence, that efficiency is not too low?

It is recommended to refer to MongoDB's objectid generation mechanism. Objectid altogether 12 bytes

  • A 4-byte timestamp, placed in the first section, allows the Objectid to "approximate" the chronological order, guaranteeing that the IDs generated for different seconds must not be duplicated
  • 3-byte machine ID, by default the machine's various basic information hash, to ensure that the same second machine generated ID must not be duplicated
  • A 2-byte process number that guarantees that the same machine will generate IDs of different processes in the same second.
  • 3 bytes of self-increment, in the same second self-increment, to ensure that the same second machine in the same process generated 16,777,216 ID must not be duplicated

You can use the UUID, if you want to change the number, then do the conversion.
Maybe the numbers will be bigger, you have to define your own rules and zoom out.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.