我想创建一个Javascript代码,可以在1-59和第6个数字1-35之间生成5个数字。
到目前为止我所拥有的:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" >
<title></title>
<script type="text/javascript">
function powerballNumber()
{
var powerballNumber = (Math.random()*59) +1;
powerballNumber = parseInt(powerballNumber);
alert(powerballNumber)
}
</script>
</head>
<body>
<form name="powerball">
<input type="button" value="Create Powerball Numbers" name="btnSubmit" onclick="powerballNumber()">
</form>
<br />
</body>
</html>
答案 0 :(得分:1)
您可以使用for
循环并使用数组在几行代码中获取此内容:
//init array
var powerBall = [];
//do you loop to get 5 numbers
for (var i = 0; i < 4; i++) {
//add them to array
powerBall.push( (Math.random()*59) +1 );
}
//and add to array 6th number
powerBall.push( (Math.random()*35) +1 );
//to access you nubmer use it like this
alert( powerBall[0] ) //from 0 to 6
答案 1 :(得分:0)
function getRandomNumbers(r) {
if (!(r instanceof Array)) return false;
var rv=[];
for (var i=0,m=r.length;i<m;i++)
rv.push(Math.floor((Math.random()*r[i]) +1));
return rv;
}
function getPowerballNumbers() {
return getRandomNumbers([59,59,59,59,59,35]);
}
powerballNumbers = getPowerballNumbers();
返回示例:
[50, 44, 6, 11, 29, 14]
答案 2 :(得分:0)
我有类似的东西,我开始制作一个插件。
基本功能是简单的设置。首先,我做了两种方法。一个用于返回选择5个数字[1-59]
的BASE数组,另一个用于获取第6个数字[1-35]
的BASE。
var pick5 = getPick5();
// return something like [ 2, 5, 9, 15, 35 ]
从方法内部的基础数组中,我可以使用方法的参数数组(如果传递)将数字添加到基数。这允许一些数字有更高的机会。通过这种方式,我可以使用户可以按顺序将3个数字设置为“幸运数字”,比如7, 5, 3
。然后我可以创建每个“幸运”数字的数组并将其传递给方法,让方法将其添加到基础数组,从而增加数字被拉的概率。最重要的是,要使一个数字“更幸运”,只需增加它在数组中的计数。
var pick5 = getPick5();
// return something like [ 2, 7, 9, 15, 35 ]
而且,最后,如果这太复杂了,你只想要六个数字,那么就这样做:
var pick6 = getPicks([ 7, 7, 7, 5, 5, 3 ]);
// return something like [ 2, 7, 9, 15, 35, 6 ]
// Notice 6 on the end, it is the "pick1" number, aka, the "red ball number"
/* Copy and past the first few functions: the rest is for show */
/* -={ BEGIN COPY }=- */
function getPick5BaseArray() {
var a = [];
for (i=1;i<=59;i++) a.push(i);
return a;
}
function getPick1BaseArray() {
var a = [];
for (i=1;i<=35;i++) a.push(i);
return a;
}
function getPick1(araAdd) {
var araP1B = getPick1BaseArray();
if (typeof araAdd == 'object' && Array.isArray(araAdd) && araAdd.length > 0) {
for (x in araAdd) {
var aX = araAdd[x],
i = araP1B.indexOf(aX);
if (i > -1) araP1B.splice(i, 0, aX);
}
}
var a = Math.floor(Math.random()*araP1B.length);
return araP1B[a];
}
function getPick5(araAdd) {
var araP5B = getPick5BaseArray(),
z = [];
if (typeof araAdd == 'object' && Array.isArray(araAdd) && araAdd.length > 0) {
for (x in araAdd) {
var aX = araAdd[x],
i = araP5B.indexOf(aX);
if (i > -1) araP5B.splice(i, 0, aX);
}
}
for (i=0;i<5;i++) {
var a = Math.floor(Math.random()*araP5B.length),
b = araP5B[a];
while (z.indexOf(b) > -1) {
a = Math.floor(Math.random()*araP5B.length);
b = araP5B[a];
}
z.push(b);
}
intArraySort(z);
return z;
}
function getPicks(ara5Add, ara1Add) {
var y = getPick1(ara1Add),
z = getPick5(ara5Add);
z.splice(z.length, 0, y)
return z;
}
function intArraySort(ara, aOd) {
var a = typeof aOd == 'string' ? aOd.toLowerCase() : aOd;
function sortIntDesc(a, b) { return b-a; }
function sortIntAsc(a, b) { return a-b; }
switch (aOd) {
case 0:
case 'a':
case 'ac':
case 'asc':
default:
return ara.sort(sortIntAsc);
case 1:
case 'd':
case 'dc':
case 'desc':
return ara.sort(sortIntDesc);
}
}
/* -={ END COPY }=- */
function setNumbers() {
var araNums = getPicks();
$("tbody td p").each(function(i) { $(this).text(araNums[i]) });
}
$(function() {
$("html, body").addClass("ui-widget ui-widget-content").css("overflow", "hidden");
setNumbers();
$("#btnReroll").on("click", setNumbers);
})
html, body, .table { color: #FFF; height: 100%; margin: 0; min-width: 100%; padding: 0; }
.table { display: table; width: 100%; }
.cell { display: table-cell; vertical-align: middle; }
table {
width: 100%;
}
td {
padding: .25em .5em;
text-align: center;
width: 3em;
}
p {
margin: 0 auto;
padding: 0;
}
.whiteball, .powerball {
height: 2em;
line-height: 2em;
width: 2em;
}
.whiteball {
background: #FFF;
color: #000;
}
.powerball {
background: #C31F3A;
color: #FFF;
}
.ui-button-text-only .ui-button-text {
font-size: .8em;
padding: .1em .2em;
}
/* Corner radius */
.ui-corner-all,
.ui-corner-top,
.ui-corner-left,
.ui-corner-tl {
border-top-left-radius: 1em;
}
.ui-corner-all,
.ui-corner-top,
.ui-corner-right,
.ui-corner-tr {
border-top-right-radius: 1em;
}
.ui-corner-all,
.ui-corner-bottom,
.ui-corner-left,
.ui-corner-bl {
border-bottom-left-radius: 1em;
}
.ui-corner-all,
.ui-corner-bottom,
.ui-corner-right,
.ui-corner-br {
border-bottom-right-radius: 1em;
}
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.1/themes/redmond/jquery-ui.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="table ">
<div class="cell ui-widget-header ui-corner-all">
<table class="">
<thead>
<tr>
<td colspan="5"><h2>Your Lucky Powerball Numbers</h2></td>
<td><button id="btnReroll" type="button">ReRoll</button></td>
</tr>
</thead>
<tbody>
<tr>
<td><p class="whiteball ui-corner-all"></p></td>
<td><p class="whiteball ui-corner-all"></p></td>
<td><p class="whiteball ui-corner-all"></p></td>
<td><p class="whiteball ui-corner-all"></p></td>
<td><p class="whiteball ui-corner-all"></p></td>
<td><p class="powerball ui-corner-all"></p></td>
</tr>
</tbody>
</table>
</div>
</div>