简介
随机整数生成器可以随机生成一个指定范围内的整数。
代码(JavaScript)
x为最小值,y为最大值,z为生成个数(x,y,z均为整数),w为是否允许重复值(布尔值),返回一个元素数目为z的数组。
function create_number(x, y, z, w) {
if (x > y || z == 0) {
return ["错误"];
}
else if (!w && y - x + 1 < z) {
return ["错误"];
}
else if (x >= 0 && y >= 0) {
var l = 0;
var yy = y;
while (yy >= 1) {
yy = yy / 10;
l++;
}
var n = Math.pow(10, l);
var arr1 = new Array();
for (var i = 0; ;) {
if (i >= z) break;
var a = Math.round(Math.random() * n);
if (a >= x && a <= y) {
if (w || arr1.indexOf(a) == -1) {
arr1.push(a);
i++;
}
}
}
}
else if (x < 0 && y <= 0) {
var l = 0;
var xx = -x;
while (xx >= 1) {
xx = xx / 10;
l++;
}
var n = Math.pow(10, l);
var arr1 = new Array();
for (var i = 0; ;) {
if (i >= z) break;
var a = -Math.round(Math.random() * n);
if (a >= x && a <= y) {
if (w || arr1.indexOf(a) == -1) {
arr1.push(a);
i++;
}
}
}
}
else if (x < 0 && y > 0) {
var l = 0;
var yy = y - x;
while (yy >= 1) {
yy = yy / 10;
l++;
}
var n = Math.pow(10, l);
var arr1 = new Array();
for (var i = 0; ;) {
if (i >= z) break;
var a = Math.round((Math.random() - 0.5) * n);
if (a >= x && a <= y) {
if (w || arr1.indexOf(a) == -1) {
arr1.push(a);
i++;
}
}
}
}
return arr1;
}
应用
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>随机整数生成器 - By 你们的饺子</title>
</head>
<body style="padding: top 10px;text-align: center;">
<div style="font-size:100px;font-weight: bold;">随机整数生成器</div>
<div style="font-size:30px;font-style:italic;">By 你们的饺子</div>
<div style="font-size:75px;font-weight: bold;" id="output">??</div>
<div>配置<br />
取值范围:从<input type="number" id="from" value="1" />到<input type="number" id="to" value="48" /><br />
生成个数:<input type="number" id="quantity" value="1" /><br />
允许重复:<input type="radio" id="repeat_t" name="repeat" checked="true" />是 <input type="radio" id="repeat_f"
name="repeat" />否
</div><br />
<button style="font-size:50px;font-weight: bold;" onclick="run();">立即生成</button>
<script>
function run() {
document.getElementById("output").innerHTML = create_number(document.getElementById("from").value * 1, document.getElementById("to").value * 1, document.getElementById("quantity").value * 1, document.getElementById("repeat_t").checked).join(" ");
}
function create_number(x, y, z, w) {
if (x > y || z <= 0) {
return ["错误"];
}
else if (!w && y - x + 1 < z) {
return ["错误"];
}
else if (x >= 0 && y >= 0) {
var l = 0;
var yy = y;
while (yy >= 1) {
yy = yy / 10;
l++;
}
var n = Math.pow(10, l);
var arr1 = new Array();
for (var i = 0; ;) {
if (i >= z) break;
var a = Math.round(Math.random() * n);
if (a >= x && a <= y) {
if (w || arr1.indexOf(a) == -1) {
arr1.push(a);
i++;
}
}
}
}
else if (x < 0 && y <= 0) {
var l = 0;
var xx = -x;
while (xx >= 1) {
xx = xx / 10;
l++;
}
var n = Math.pow(10, l);
var arr1 = new Array();
for (var i = 0; ;) {
if (i >= z) break;
var a = -Math.round(Math.random() * n);
if (a >= x && a <= y) {
if (w || arr1.indexOf(a) == -1) {
arr1.push(a);
i++;
}
}
}
}
else if (x < 0 && y > 0) {
var l = 0;
var yy = y - x;
while (yy >= 1) {
yy = yy / 10;
l++;
}
var n = Math.pow(10, l);
var arr1 = new Array();
for (var i = 0; ;) {
if (i >= z) break;
var a = Math.round((Math.random() - 0.5) * n);
if (a >= x && a <= y) {
if (w || arr1.indexOf(a) == -1) {
arr1.push(a);
i++;
}
}
}
}
return arr1;
}
</script>
</body>
</html>
Great ,VERY NICE !
Well,well,well.