Javascript加权概率数组 [英] Javascript Weighted Probability Array

查看:58
本文介绍了Javascript加权概率数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一款在浏览器中运行的非常基本的游戏。文档准备好后,我创建了一些Javascript,它显示了一个来自较大链接列表的随机链接(两个列表都存储在数组中)。我的问题有两个部分 - 一个简单,一个复杂:

I'm working on a very basic game which runs in the browser. When the document is ready, I have created some Javascript which displays a random couple of links from a larger link list (both lists are stored in Arrays). My question has two parts - one simple, one complex:

1)是否有办法使这个代码更优雅 - 数组中的数组内的函数非常丑陋...

1) Is there a way to make this code more elegant - the function within the array within the array is very ugly...

2)有没有办法为项目分配概率,以便某些项目比其他项目更有可能出现?目前它只是随机的,任何按钮都可能出现在任何其他地方。

2) Is there a way to allocate probabilities to the items so that some are more likely to appear than others? At the moment it is just random, with any "button" as likely to appear as any other.

这是我的代码:

HTML

<body>

<h1 id="OTM_Test">Javascript Probability</h1>

<div id="stackoverflow">

<a href="nextpage.html" id="button1" class="button">Next Page 1</a>
<a href="nextpage.html" id="button2" class="button">Next Page 2</a>
<a href="nextpage.html" id="button3" class="button">Next Page 3</a>
<a href="nextpage.html" id="button4" class="button">Next Page 4</a>
<a href="nextpage.html" id="button5" class="button">Next Page 5</a>
<a href="nextpage.html" id="button6" class="button">Next Page 6</a>

</div>

</body>

CSS
.button {
背景:蓝色;
颜色:白色;
display:none;
保证金:200px;
填充:10px;
display:none;
border:2px solid red;
}

Javascript / jQuery

$(document).ready(function(){

var button1 = $("#button1");
var button2 = $("#button2");
var button3 = $("#button3");
var button4 = $("#button4");
var button5 = $("#button5");
var button6 = $("#button6");

var totalArray = [button1,button2,button3,button4,button5,button6];



function randomChoice(){
    var randomNumber=Math.floor(Math.random()*6);
    return randomNumber;
    };



var selectedArray = [(totalArray[(randomChoice())]), (totalArray[(randomChoice())])];

function reduceArray(){
    for(i=0;i<selectedArray.length;i++){
        (selectedArray[i]).show();
        }
};

reduceArray();

});

我们将非常感谢您的任何想法/想法。

Any ideas/thoughts would be gratefully received.

这是我在这里的第一篇文章...请保持温和!

It is my first post on here...please be gentle!

推荐答案

这是一个更整洁的版本:

this is a more neat version:

$(document).ready(function(){
 var totalArray = [];
    $('#stackoverflow').children().each(function(){
    totalArray.push($(this)):
    });

    function randomChoice(){
        var randomNumber=Math.floor(Math.random()*3)+Math.floor(Math.random()*3); //middle buttons have more chance of apearing
        return randomNumber;
    };

    var selectedArray = [totalArray[(randomChoice()], totalArray[randomChoice()]];
    $.each(selectedArray,function(index,object){
       $(object).show();
    }

});

甚至更短的代码:

function randomChoice(){
    var randomNumber=Math.floor(Math.random()*3)+Math.floor(Math.random()*3); //middle buttons have more chance of apearing
    return randomNumber;
};

$('#button'+ randomChoice()).show():
$('#button'+ randomChoice()).show():

您可以设置html数据属性的概率:

for probability you could set html data attribute:

<a href="nextpage.html" id="button1" class="button" data-change="20">Next Page 1</a>
.. means 20% change of apearing


var total = 0;
$('#stackoverflow').children().each(function(){
    totalArray.push($(this)):
    if($(this).data('change')>Math.random()*100){
      $(this).show();
    }
    });

这篇关于Javascript加权概率数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆