Skip to content

Instantly share code, notes, and snippets.

View dingosky's full-sized avatar

Paul Rogers dingosky

  • dingo sky
  • Güéjar-Sierra, Granada, Spain
  • 03:21 (UTC +02:00)
View GitHub Profile
@dingosky
dingosky / explicit.ex
Created July 31, 2022 21:48
Explicit randomness
# Elixir
defmodule(RandId, do: use(Puid, total: 500_000, risk: 1.0e12, chars: :alphanum_lower))
@dingosky
dingosky / dbId.js
Last active July 31, 2022 21:42
DB IDs
// JavaScript
const { puid } = require('puid-js')
const { generator: dbId } = puid({ total: 1000, risk: 1e15 })
dbId()
// => 'c1DVnnbI3RTr'
@dingosky
dingosky / CommonId.swift
Last active January 27, 2023 17:00
Common random ID strategy
// Swift
let chars = (UInt8(ascii: "a")...UInt8(ascii: "z")).map { $0 }
func commonId(len: Int) -> String {
String(bytes: (0..<len)
.map { $0 }
.map { _ in
chars[Int.random(in: 0..<chars.count)]
}, encoding: .ascii)!
}
commonId(len: 8)
@dingosky
dingosky / OneBitRandom.swift
Last active January 26, 2023 11:02
1 bit random ID
// Swift
func randId() -> String {
Bool.random() ? "18f6303a" : "1"
}
randId()
// -> "18f6303a"