Skip to content

Instantly share code, notes, and snippets.

@amfg
Created February 7, 2016 13:30
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amfg/e007f5ee0e3d2d981897 to your computer and use it in GitHub Desktop.
Save amfg/e007f5ee0e3d2d981897 to your computer and use it in GitHub Desktop.
Generate random unicode string
random = function(length) {
var array = new Uint16Array(length);
window.crypto.getRandomValues(array);
var str = '';
for (var i = 0; i < array.length; i++) {
str += String.fromCharCode(array[i]);
};
return str;
}
@Bug-Reaper
Copy link

5 years later, this seems to work. Not sure if it covers the entire unicode space but it's clean.

@Lagyu
Copy link

Lagyu commented Oct 8, 2020

const randomUnicodeString = (length: number): string => Array.from(
  { length }, () => String.fromCharCode(Math.floor(Math.random() * (65536)))
).join('')

another version :)

@AddisonTheCat
Copy link

noicee

@RossRogers
Copy link

What about

const random_unicode_string = (length: number): string =>
      String.fromCharCode.apply(null, Array.from({length}, () => Math.floor(Math.random()*65536)))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment