Scholar.randomString( [int len] ) -- generate a random string

This commit is contained in:
Dan Stillman 2006-06-01 19:46:57 +00:00
parent c2c0f0f614
commit 0d27815694

View file

@ -21,6 +21,7 @@ var Scholar = new function(){
this.getString = getString;
this.flattenArguments = flattenArguments;
this.join = join;
this.randomString = randomString;
this.Hash = Hash;
@ -177,6 +178,23 @@ var Scholar = new function(){
}
/**
* Generate a random string of length 'len' (defaults to 8)
**/
function randomString(len) {
var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
if (!len){
len = 8;
}
var randomstring = '';
for (var i=0; i<len; i++) {
var rnum = Math.floor(Math.random() * chars.length);
randomstring += chars.substring(rnum,rnum+1);
}
return randomstring;
}
/*
* Class for creating hash arrays that behave a bit more sanely
*