¿Es posible crear una cadena alfanumérica aleatoria de 10 caracteres sin colisiones? Por cierto, tiene que estar todo en minúsculas. Bitly parecía haberlo resuelto, pero sé que usan una combinación de mayúsculas y minúsculas que aumenta la aleatoriedad. Sin embargo, esto tiene que hacerse con un solo caso.
Aquí hay una función para aleatorizar cadenas y un intento honesto de encontrar duplicados.
function random_str(length, possible) { var text = ""; length = length || 5; possible = possible || "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; for (var i = 0; i < length; i++) text += possible.charAt(Math.floor(Math.random() * possible.length)); return text; }; var max = 1e4; var i = 0; var arr = []; while (true) { var id = random_str(10, "abcdefghijklmnopqrstuvwxyz0123456789"); console.log(id) if (arr.indexOf(id) > -1) { alert("end of universe"); break; } arr.push(id); if (i++ >= max) { break; } } console.log("done. no duplicates found after " + i + " attempts") .as-console-wrapper { max-height: 100% !important; }