Skip to content

Instantly share code, notes, and snippets.

@oieioi
Last active December 19, 2015 04:29
Show Gist options
  • Select an option

  • Save oieioi/5897875 to your computer and use it in GitHub Desktop.

Select an option

Save oieioi/5897875 to your computer and use it in GitHub Desktop.
String shuffle and random and
// 文字列シャッフル
String.prototype.shuffle = function() {
var result = [];
var ary = this.split("");
var l = ary.length;
for (var i = 0;i < l;i++) {
var random = Math.floor(Math.random()*ary.length);
result.push(ary[random]);
ary.splice(random,1);
}
return result.join("");
};
// 文字列をcount数分ランダムで返す
String.prototype.random = function(count) {
var result = [];
var ary = this.split("");
var l = count || ary.length;
for (var i = 0;i < l;i++) {
var random = Math.floor(Math.random()*ary.length);
result.push(ary[random]);
}
return result.join("");
};
// 文字列中に含まれる各文字の数をカウントする
String.prototype.charCount =function() {
var l = this.length;
var result = {};
for (var i = 0;i<l ;i++) {
if (result[this[i]] == undefined) {
result[this[i]] = 0;
}
result[this[i]] = result[this[i]] + 1;
}
return result;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment