Fancy DSA Fancy DSA
数据结构
算法
LeetCode
  • 关于
  • 导航 (opens new window)
  • 分类
  • 标签
  • 归档
设计模式 (opens new window)
博客 (opens new window)
GitHub (opens new window)

Jonsam NG

想的更多,也要想的更远
数据结构
算法
LeetCode
  • 关于
  • 导航 (opens new window)
  • 分类
  • 标签
  • 归档
设计模式 (opens new window)
博客 (opens new window)
GitHub (opens new window)
  • 开始上手
  • Plan 计划
  • Roadmap 路线
  • 算法简介
  • Sort 排序

  • Search 搜索

  • Recursive 递归

  • Graph 图

  • Tree 树

  • Math 数学

  • Hash 哈希

  • String 字符串

    • AlphaNumericPalindrome [回文串]
    • AlternativeStringArrange [交替合并字符串]
    • BoyerMoore [博耶-穆尔字符串搜索算法、BM 算法]
    • CheckAnagram [易位构词]
    • NamingConvention [命名规则]
    • CheckExceeding [Exceeding words]
    • CheckPangram [全字母句]
    • CheckWordOccurrence [单词计数]
    • CountVowels [元音字母计数]
    • CreatePermutations [全排列]
    • DiceCoefficient [Dice系数]
    • FormatPhoneNumber [格式化电话号码]
    • GenerateGUID [生成GUID、UUID]
      • 介绍
      • GUID 和 UUID de 区别
      • 实现
      • 参考
    • HammingDistance [汉明距离]
    • KMPPatternSearching [KMP字符串匹配]
  • BitManipulation 位操纵

  • Backtracking 回溯

  • DynamicProgramming 动态规划

  • Cache 缓存

  • Array 数组

  • Ciphers 密码学

  • Conversions 转换

  • ProjectEuler 欧拉计划

  • 其他

  • 算法
  • String 字符串
jonsam
2022-09-26
目录

GenerateGUID [生成GUID、UUID]

# 介绍

通用唯一识别码(英语:Universally Unique Identifier,缩写:UUID)是用于计算机体系中以识别信息的一个 128 位标识符。

根据标准方法生成,不依赖中央机构的注册和分配,UUID 具有唯一性,这与其他大多数编号方案不同。重复 UUID 码概率接近零,可以忽略不计。

# GUID 和 UUID de 区别

把它们当作一个 16 字节(128 位)的值,作为一个唯一的值使用。在微软的说法中,它们被称为 GUID,但在不使用微软的说法时,就叫它们 UUID。甚至 UUID 规范的作者和微软都声称它们是同义词。参考:Is there any difference between a GUID and a UUID? - Stack Overflow (opens new window)。

# 实现

# JavaScript

/*
Generates a UUID/GUID in Node.Js.
The script uses `Math.random` in combination with the timestamp for better randomness.
The function generate an RFC4122 (https://www.ietf.org/rfc/rfc4122.txt) version 4 UUID/GUID
*/

const Guid = () => {
  const pattern = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'
  let currentDateMilliseconds = new Date().getTime()
  return pattern.replace(/[xy]/g, currentChar => {
    const randomChar = (currentDateMilliseconds + Math.random() * 16) % 16 | 0
    currentDateMilliseconds = Math.floor(currentDateMilliseconds / 16)
    return (currentChar === 'x' ? randomChar : (randomChar & 0x7 | 0x8)).toString(16)
  })
}
// > Guid()
// 'edc848db-3478-1760-8b55-7986003d895f'
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  • | 0 向下取整(负数则向上取整),相当于 ~~n 或者 Math.foloor(n) 。

# 参考

  • Universally unique identifier - Wikiwand (opens new window)
  • 通用唯一识别码 - Wikiwand (opens new window)
编辑 (opens new window)
上次更新: 2022/10/20, 20:45:42
FormatPhoneNumber [格式化电话号码]
HammingDistance [汉明距离]

← FormatPhoneNumber [格式化电话号码] HammingDistance [汉明距离]→

最近更新
01
0-20题解
10-31
02
本章导读
10-31
03
算法与转换:Part1
10-28
更多文章>
Theme by Vdoing | Copyright © 2022-2022 Fancy DSA | Made by Jonsam by ❤
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式