Home > Web Front-end > JS Tutorial > js code for random selection according to given probability_javascript skills

js code for random selection according to given probability_javascript skills

WBOY
Release: 2016-05-16 18:13:05
Original
1168 people have browsed it
复制代码 代码如下:

function StringResource(k) { return StringResource[k] || k; }
function RandomSelector() {
var f = 0, c = [];

return function (v, r) {
if (arguments.length) {
if (r <= 0) throw { message: StringResource("INVALID_ARGUMENT") };
c.push([r, v]);
f = r;
} else {
var i = 0; r = Math.random() * f;
for (; ; ) if ((r -= (v = c[i ])[0]) < 0) return v[1];
}
};
}
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template