html获取随机字母,HTML5 随机英文单词生成器+字母出现概率

JavaScript

语言:

JaveScriptBabelCoffeeScript

确定

(function() {

var dict, randomAtoZ, randomWord, randomWordLength, word_history, wordfreq, writeWord;

dict = [];

$.get('/uploads/150101/dict-eng.txt', function(txt) {

var i, words;

words = txt.split('\n');

i = 0;

while (i < words.length) {

dict.push(words[i]);

i++;

}

writeWord(randomWord());

});

randomAtoZ = function(lookup) {

return function() {

var chance, char, charfreq, prev, random;

random = Math.random() * 100000;

char = void 0;

prev = 0;

for (char in lookup) {

charfreq = lookup[char];

chance = (charfreq - prev) / 1000 + '%';

if (random < charfreq) {

return {

char: char,

charfreq: charfreq,

chance: chance

};

}

prev = charfreq;

}

};

}({

a: 8167,

b: 9659,

c: 12441,

d: 16694,

e: 29396,

f: 31624,

g: 33639,

h: 39733,

i: 46699,

j: 46852,

k: 47624,

l: 51649,

m: 54055,

n: 60804,

o: 68311,

p: 70240,

q: 70335,

r: 76322,

s: 82649,

t: 91705,

u: 94463,

v: 95441,

w: 97801,

x: 97951,

y: 99925,

z: 100000

});

wordfreq = [];

randomWordLength = function() {

var amount, chance, length, lengthfreq, lookup, percent, percentages, prev, random, total, _i, _len;

total = 0;

percentages = [

0.1,

0.6,

2.6,

5.2,

8.5,

12.2,

14,

14,

12.6,

10.1,

7.5,

5.2,

3.2,

2,

1,

0.6,

0.3,

0.2,

0.1

];

for (_i = 0, _len = percentages.length; _i < _len; _i++) {

percent = percentages[_i];

amount = total + percent / 100 * 100000;

wordfreq.push(amount);

total = amount;

}

random = Math.random() * 100000;

length = void 0;

lookup = {

1: wordfreq[0],

2: wordfreq[1],

3: wordfreq[2],

4: wordfreq[3],

5: wordfreq[4],

6: wordfreq[5],

7: wordfreq[6],

8: wordfreq[7],

9: wordfreq[8],

10: wordfreq[9],

11: wordfreq[10],

12: wordfreq[11],

13: wordfreq[12],

14: wordfreq[13],

15: wordfreq[14],

16: wordfreq[15],

17: wordfreq[16],

18: wordfreq[17],

19: wordfreq[18]

};

prev = 0;

for (length in lookup) {

lengthfreq = lookup[length];

chance = Math.round(lengthfreq - prev) / 1000 + '%';

if (random < lengthfreq) {

return {

length: length,

chance: chance

};

}

prev = lengthfreq;

}

};

randomWord = function() {

var chance, char_map, character, i, isword, length, length_i, word, _i;

length = randomWordLength();

length_i = length.length;

chance = length.chance;

word = '';

char_map = [];

for (i = _i = 1; 1 <= length_i ? _i <= length_i : _i >= length_i; i = 1 <= length_i ? ++_i : --_i) {

character = randomAtoZ();

word += character.char;

char_map.push(character);

}

if ($.inArray(word, dict) > -1) {

alert('"' + word + '" is a word in the dictionary. Congrats.');

isword = true;

} else {

isword = false;

}

return {

length_i: length_i,

chance: chance,

word: word,

isword: isword,

char_map: char_map

};

};

word_history = [];

writeWord = function(generated_word) {

var char, charlist, charlist_html, history_string, isword, length, w, word, _i, _j, _len, _len1, _ref;

document.getElementsByTagName('body')[0].className = '';

if (generated_word.isword === true) {

isword = 'isword';

} else {

isword = 'isnotword';

}

word = document.getElementById('word');

word.innerHTML = generated_word.word;

document.getElementsByTagName('body')[0].className = isword;

length = document.getElementById('length');

length.innerHTML = generated_word.length_i + ' (' + generated_word.chance + ')';

charlist = document.getElementById('charlist');

charlist_html = '';

_ref = generated_word.char_map;

for (_i = 0, _len = _ref.length; _i < _len; _i++) {

char = _ref[_i];

charlist_html += '

' + char.char + ' ' + char.chance + '';

}

charlist.innerHTML = charlist_html;

word_history.unshift({

word: generated_word.word,

isword: generated_word.isword

});

history_string = '';

for (_j = 0, _len1 = word_history.length; _j < _len1; _j++) {

w = word_history[_j];

if (w.isword === true) {

isword = 'isword';

} else {

isword = 'isnotword';

}

history_string += '

' + w.word + '';

}

return document.getElementById('history').innerHTML = history_string;

};

document.getElementById('new_word').onclick = function() {

return writeWord(randomWord());

};

}.call(this));

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值