250x250
Rainbow๐ŸŒˆCoder
My dev Note๐Ÿ“’
Rainbow๐ŸŒˆCoder
์ „์ฒด ๋ฐฉ๋ฌธ์ž
์˜ค๋Š˜
์–ด์ œ
  • ๋ถ„๋ฅ˜ ์ „์ฒด๋ณด๊ธฐ (411)
    • ๊ณต์ง€์‚ฌํ•ญ (0)
    • Debugger (10)
      • Visual Studio Debugger (1)
      • Chrome DevTools (3)
      • Visual Studio Code Debugger (4)
      • eclipse (1)
      • intelliJ (1)
    • OOP (2)
      • OOP (2)
    • TypeScript (54)
      • ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ TypeScript (54)
    • Javascript (87)
      • Javascript (45)
      • Node.js (19)
      • React (5)
      • FE ๊ฐœ๋ฐœํ™˜๊ฒฝ์„ค์ • (3)
      • React์™€ Node ๊ฐ™์ด ๋•Œ๋ ค์žก๊ธฐ (6)
      • next.js (2)
      • pixi.js (7)
    • ๋งˆํฌ์—… (23)
      • Html & Css (23)
    • C# (80)
      • C# (12)
      • ์ด๊ฒƒ์ด C#์ด๋‹ค (68)
    • C++ (30)
      • c++ (27)
      • win api (3)
    • Unity (18)
      • Unity(๊ธฐ์ดˆ) (8)
      • Unity(C#์ค‘๊ธ‰) (5)
      • ์œ ๋‹ˆํ‹ฐ ํฌํ†ค(๋„คํŠธ์›Œํฌ) (4)
      • unity c# MyCode (1)
    • Java & Spring (29)
      • Java (11)
      • ์Šคํ”„๋ง (8)
      • Java Algorithm (9)
      • Javs Data Structures (1)
    • ์ž๋ฃŒ๊ตฌ์กฐ์™€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ (15)
      • ์ž๋ฃŒ๊ตฌ์กฐ (5)
      • ์•Œ๊ณ ๋ฆฌ์ฆ˜ (10)
    • ํ˜•์ƒ๊ด€๋ฆฌ (15)
      • Git (11)
      • ์†Œ์ŠคํŠธ๋ฆฌ (3)
    • ๊ทธ๋ž˜ํ”ฝ์Šค (7)
      • WebGl (7)
    • AWS (3)
      • aws (3)
    • ๋ฆฌ๋ˆ…์Šค (5)
      • ๋ฆฌ๋ˆ…์Šค (5)
    • ์ฑ… ๋ฆฌ๋ทฐ (13)
      • ํด๋ฆฐ์ฝ”๋“œ(์ฑ…๋ฆฌ๋ทฐ) (3)
      • ์œ ์ง€๋ณด์ˆ˜๊ฐ€๋Šฅํ•œ์ฝ”๋”ฉ์˜๊ธฐ์ˆ C#ํŽธ(์ฑ…๋ฆฌ๋ทฐ) (1)
      • ๋ฆฌํŒฉํ† ๋ง(์ž๋ฐ”์Šคํฌ๋ฆฝํŠธํŒ) (9)
    • Server (2)
      • ๊ฒŒ์ž„ ์„œ๋ฒ„(๋„คํŠธ์›Œํฌ, ๋ฉ€ํ‹ฐ์“ฐ๋ ˆ๋“œ,OS) (2)
    • ์„ค๊ณ„, ์•„ํ‚คํ…์ณ (4)
    • ํŒŒ์ด์ฌ (5)
    • ๋””์ž์ธํŒจํ„ด (2)
    • mocha (2)
    • Jest (1)
    • Spine (1)
    • ์ธ๊ณต์ง€๋Šฅ (1)
      • ํ˜ผ์ž๊ณต๋ถ€ํ•˜๋Š”๋จธ์‹ ๋Ÿฌ๋‹+๋”ฅ๋Ÿฌ๋‹ (1)

๋ธ”๋กœ๊ทธ ๋ฉ”๋‰ด

  • ํ™ˆ
  • ํƒœ๊ทธ
  • ๋ฐฉ๋ช…๋ก

๊ณต์ง€์‚ฌํ•ญ

์ธ๊ธฐ ๊ธ€

ํƒœ๊ทธ

  • MySQL
  • ใ…ฃใ„ท
  • ์œ„์ž„
  • ์ปดํฌ์ง€์…˜

์ตœ๊ทผ ๋Œ“๊ธ€

์ตœ๊ทผ ๊ธ€

ํ‹ฐ์Šคํ† ๋ฆฌ

hELLO ยท Designed By ์ •์ƒ์šฐ.
Rainbow๐ŸŒˆCoder

My dev Note๐Ÿ“’

Javascript/Javascript

generate random id ์— ๋Œ€ํ•œ ๊นƒํ—ˆ๋ธŒ ๋‹ต๋ณ€

2022. 4. 17. 13:15
728x90

ID - a unique ID/name generator for JavaScript (github.com)

 

ID - a unique ID/name generator for JavaScript

ID - a unique ID/name generator for JavaScript. GitHub Gist: instantly share code, notes, and snippets.

gist.github.com

I strongly recommend using performance.now().toString(36) instead of Date.now - since it offers a higher precision date (ref: https://developer.mozilla.org/en-US/docs/Web/API/Performance/now - you can get microsecond precision).

The advantage of a higher precision time is: the probability of users around the known universe generating their random ID in that exact same microsecond is very low.

Doing Math.random() in addition to this will result in randomization over-and-above an already extremely unique value.

function uid() {
  return (performance.now().toString(36)+Math.random().toString(36)).replace(/\./g,"");
};

Verified using @c0d3r111's Collision test
Collision test: 8391.403076171875ms
Total iterations: 1000000
Total collisions: 0
Total unique ids: 1000000

Unless your CPU can process more instructions in a microsecond, the value will likely never duplicate since you've moved into the next microsecond by the time the result comes out.

Window.crypto (ref https://developer.mozilla.org/en-US/docs/Web/API/Window/crypto ) as @lcherone also mentioned, provides high precision random numbers, that even inspires confidence in Cryptographic algorithms (and is thus assured to be more random than other randoms), which can be used as such:

function getRandomNumbers() {
  var array = new Uint32Array(10);
  window.crypto.getRandomValues(array);
 
  for (var i = 0; i < array.length; i++) {
    console.log(array[i] + " ");
  }
}

Combining the two, here's a final function:

function uid() {
 let a = new Uint32Array(3);
 window.crypto.getRandomValues(a);
 return (performance.now().toString(36)+Array.from(a).map(A => A.toString(36)).join("")).replace(/\./g,"");
};

 

728x90

'Javascript > Javascript' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ] ์ œ๋„ˆ๋ ˆ์ดํ„ฐ  (0) 2022.04.19
[์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ] how to remove array element javascript  (0) 2022.04.17
์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋ฐ˜๋ณต๋ฌธ  (0) 2022.04.14
[๋“œ๋ฆผ์ฝ”๋”ฉ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ] ๋น„๋™๊ธฐ promise  (0) 2022.04.13
[๋“œ๋ฆผ์ฝ”๋”ฉ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ]๋น„๋™๊ธฐ์˜ ์‹œ์ž‘ ์ฝœ๋ฐฑ์ง€์˜ฅ  (0) 2022.04.13
    'Javascript/Javascript' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
    • [์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ] ์ œ๋„ˆ๋ ˆ์ดํ„ฐ
    • [์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ] how to remove array element javascript
    • ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋ฐ˜๋ณต๋ฌธ
    • [๋“œ๋ฆผ์ฝ”๋”ฉ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ] ๋น„๋™๊ธฐ promise
    Rainbow๐ŸŒˆCoder
    Rainbow๐ŸŒˆCoder
    ๋ชฐ๋ผ๋„ ๊ฒฐ๊ตญ์€ ์•„๋Š” ๊ฐœ๋ฐœ์ž, ๊ทธ๋Ÿฐ ์‚ฌ๋žŒ์ด ๋˜๊ธฐ ์œ„ํ•œ ๋งค์ผ์˜ ํ•œ๊ฑธ์Œ

    ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”