-4

我想创建可以产生 1-59 之间的 5 个数字和 1-35 之间的第 6 个数字的 Javascript 代码。

到目前为止我所拥有的:

<!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>
4

3 回答 3

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 
于 2013-11-02T19:49:44.727 回答
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]

于 2013-11-02T19:59:21.150 回答
0

我有类似的东西我开始为我制作的插件。

基本功能是一个简单的设置。首先我做了2个方法。一个用于返回选择 5 个数字的 BASE 数组,[1-59]一个用于获取第 6 个数字的 BASE [1-35]

然后,我分别设计了一个简单的方法,以使用前面的方法制作一个基本变量数组。所以要得到一个基本的选择5:

var pick5 = getPick5();
// return something like [ 2, 5, 9, 15, 35 ]

从方法内部的基本数组中,我可以使用方法的参数数组(如果传递)将数字添加到基数。这允许一些数字有更高的机会。这样我就可以做到,用户可以7, 5, 3按顺序将 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>

于 2014-12-07T21:46:51.127 回答