Random words, numbers and characters with Javascript

I needed a fast and easy way to generate a couple (5000) of strings containing random characters and I came with the following:

function randStrings(howMany, howLong) {
// define a string with valid characters
var characters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZ"+
"abcdefghiklmnopqrstuvwxyz";
for (var i=0;i<howMany;i++)
{
var word="";
for (var j=0;j<howLong;j++)
{
var rand = Math.floor(Math.random() * characters.length);
word += characters.substring(rand,rand+1);
}
document.writeln(word);
}
}

Share and Enjoy:These icons link to social bookmarking sites where readers can share and discover new web pages.
  • blinkbits
  • BlinkList
  • del.icio.us
  • digg
  • Fark
  • Furl
  • Reddit
  • Spurl
  • YahooMyWeb

Leave a comment

Please be polite and on topic. Your e-mail will never be published.