简体   繁体   English

在JS中获取随机数

[英]Getting a random number in JS

I am little bit new to JS. 我对JS有点陌生。 I have code which constantly play bets on a certain game, and I am trying to make it skip a random number of sessions from an array I have set. 我有在特定游戏中不断下注的代码,并且我试图使其跳过设置的数组中的随机会话数。

Right now it skips 4 sessions exactly for some reason. 现在,由于某种原因,它完全跳过了4个会话。 what am I doing wrong? 我究竟做错了什么?

I would also love to know how can I display the picked number in the console as well. 我也很想知道如何在控制台中显示选择的号码。

/** -------------- Settings -------------- **/

var array = [0,1,2,3,4,5];
var num = Math.floor(Math.random() * array.length);
var roll = array.splice(num, 1);
var yourNumber = roll[ 0 ];

var settings = {

    'baseBet': 1,
    //Your base bet

    'nyanMultiplier': 1.10,
    //What multiplier would you like to grab

    'waitGames': yourNumber,
    //How many games should we wait before starting the bet

};
/** -------------- Settings -------------- **/


var script = {
    'totalWaited': 0,
    'placingBet': false,
    'attempts': 0
};

engine.on('game_starting', function(info)
{

    if(script.totalWaited >= settings.waitGames)
    {
        script.placingBet = true;
        engine.placeBet(Math.round(settings.baseBet) * 100, Math.round(settings.nyanMultiplier * 100), false);
        log('Placing bet now');
    }
    else
    {
        log('Still waiting before we place the bet.');
        script.placingBet = false;
    }

    if(script.placingBet)
    {
        if (engine.lastGamePlay() == 'LOST')
        {
            log('Shot and a miss, maybe next game <3');
        }
        else
        {
            log('YO WE GOT IT. GG <3');
            script.totalWaited = 0;
            script.attempts = 0;
            settings.waitGames;

        }
    }

});

engine.on('game_started', function(data)
{
    if(!script.placingBet)
    {
        script.totalWaited++;
    }
    else
    {
        script.attempts++;
    }
});

function log(message)
{
    console.log('[Bot] ' + message);
}

Your array's length is 6, and therefore: Math.floor(Math.random() * array.length); 您的数组长度为6,因此: Math.floor(Math.random() * array.length); will create a number between 0-5. 将在0-5之间创建一个数字。

That 'splice' will not only retrieve the amount of cells (1 in your case) from the array, but also mutate the original array and remove those cells. 该“拼接”不仅会从数组中检索单元格的数量(在您的情况下为1),还会对原始数组进行突变并删除这些单元格。

Unless this is intentional, use slice . 除非有意,否则请使用slice

yourNumber can possibly be 4, but your code is right so it should actually be a random number from that array. yourNumber可以为4,但您的代码正确,因此它实际上应该是该数组中的随机数。 From here moving to your second request - console.log(yourNumber) is how you would print it to the available console (as I can see is already implemented in the log function). 从这里移至第二个请求console.log(yourNumber)是将其打印到可用控制台的方式(如我所见,已在log函数中实现)。

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

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