简体   繁体   English

javascript,在每个奇异数组中获取随机数

[英]javascript, getting a random number result in each singular array

I'm not sure how to word the question and i'm still quite new at javascript. 我不确定该如何措辞,我在javascript上还是很新的。 So I've got a random quote generator that has each quote result as an array. 所以我有一个随机的报价生成器,它把每个报价结果作为一个数组。 I'd like to add in two items in the array which I've got so far but having one result be a random number generated eg "2 quote" but having 2 be randomised each time. 我想在数组中添加两个项目,到目前为止,但是有一个结果是生成的随机数,例如“ 2 quote”,但是每次都有2个被随机化。 The end result is for a browser based text game. 最终结果是基于浏览器的文字游戏。 So it could be "2 zombies attack" or "7 zombies attack." 因此它可能是“ 2个僵尸攻击”或“ 7个僵尸攻击”。 The code I have so far is: 到目前为止,我的代码是:

var quotes = [
[x, 'Zombies attack!'],
[x, 'other creatures attack'],
['next line'],
]  


   function newQuote() {
var randomNumber = Math.floor(Math.random() * (quotes.length));
document.getElementById('quote').innerHTML = quotes[randomNumber];  
}

Ideally need x(or i however it's going to work) to be the result of a random number between a set range, each differently each array. 理想情况下,需要x(或i,但是它将起作用)是设置范围之间的随机数的结果,每个范围各不相同。

Thank you ps I forgot to mention that not all the quotes require a number. 谢谢ps我忘了提到并非所有引号都需要数字。 Thats why I've done it as a double array. 这就是为什么我将其作为双数组来完成。

If I understand your goal correctly, you want to have a set of similar-ish message templates, pick one of them at some point and fill it with data, correct? 如果我正确地理解了您的目标,那么您想要一组类似消息的模板,在某个时候选择其中一个模板,并用数据填充它,对吗? There's a lot of ways to tackle this problem, depending on how varying can your templates be. 有很多方法可以解决此问题,具体取决于模板的变化程度。 For a simple case in my head where you just need to prepend a number to a string I'd do something like this: 对于我脑海中的一个简单情况,您只需要在字符串前添加数字,我会执行以下操作:

var messages = [" zombies attack",
        " other creatures attack"], // define your messages
        messageIndex = Math.floor(Math.random() * messages.length), // pick one of them
        numberOfMonsters = Math.floor(Math.random() * 10 + 1), // get your random number
        result = numberOfMonsters + messages[messageIndex]; // construct a resulting message

document.getElementById('quote').textContent = result;

If you'd rather have more complex strings where you don't necessarily add a number (or any string) to the beginning, like ["There's X things in the distance", "X things are somewhere close"] , then I'd recommend to either come up with some sort of string formatting of your own or use a library to do that for you. 如果您希望使用更复杂的字符串,而不必在开头添加数字(或任何字符串),例如["There's X things in the distance", "X things are somewhere close"] ,那么我d建议要么提出自己的某种字符串格式,要么使用库为您完成此操作。 sprintf.js seems to be just right for that, it will let you do things like this: sprintf.js似乎恰好适合于此 ,它可以让您执行以下操作:

var messages = ["%d zombies attack",
        "A boss with %d minions attacks"], // define your messages
        messageIndex = Math.floor(Math.random() * messages.length), // pick one of them
        numberOfMonsters = Math.floor(Math.random() * 10 + 1), // get your random number
        result = sprintf(messages[messageIndex], numberOfMonsters) // format a final message
document.getElementById('quote').textContent = result;

EDIT: Your task is much more complex than what is described in the original question. 编辑:您的任务比原始问题中描述的要复杂得多。 You need to think about you code and data organization. 您需要考虑您的代码和数据组织。 You have to outline what is finite and can be enumerated (types of actions are finite: you can loot, fight, move, etc.), and what is arbitrary and dynamic (list of monsters and loot table are arbitrary, you have no idea what type and amount of monsters game designers will come up with). 您必须概述什么是有限的并且可以枚举(动作的类型是有限的:可以进行战利品,战斗,移动等),什么是任意的和动态的(怪物列表和战利品表是任意的,您不知道游戏设计师会想出哪种类型和数量的怪物)。 After you've defined your structure you can come up with some quick and dirty message composer, which takes arbitrary entities and puts them into finite amount of contexts, or something. 定义好结构后,您可以想到一些快速而肮脏的消息编写器,该消息编写器可以接收任意实体并将它们置于有限数量的上下文中。 Again, I'm sort of shooting in the dark here, but here's an updated version of the code on plunkr . 再次,我在这里有点黑暗,但是这里是plunkr上代码的更新版本

I solved it to do what I want and still have the numbers different. 我解决了该问题以完成我想要的事情,但数字仍然不同。 The issue was I should have had the number generator within the quote function. 问题是我应该在quote函数中使用数字生成器。 Also can create multiple variables to use too for different number generators. 也可以创建多个变量以供不同的数字生成器使用。 The plan is to then integrate it with php to add content dynamically. 该计划是然后将其与php集成以动态添加内容。 Which I can do. 我能做的。 Thanks Dmitry for guiding me in the right direction. 感谢德米特里(Dmitry)指导我朝正确的方向发展。

function newQuote() {

var MonsterOne = Math.floor((Math.random() * 14) + 0);
var MonsterTwo = Math.floor((Math.random() * 14) + 0);
var MonsterThree = Math.floor((Math.random() * 14) + 0);
var MonsterFour = Math.floor((Math.random() * 14) + 0);

    var quotes = [

['Test', MonsterOne, 'One'],
['Test', MonsterOne,'Two'],
['Test', MonsterThree, 'Three'],
[MonsterFour, 'Four'],
['Five'],

]


var randomNumber = Math.floor(Math.random() * (quotes.length));
document.getElementById('quote').innerHTML = quotes[randomNumber];  
}  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM