【问题标题】:Random boolean 70/30 inside function?内部函数的随机布尔值 70/30?
【发布时间】:2019-02-04 21:24:01
【问题描述】:

我正在控制台中运行一个脚本,以便使用以下脚本在 Tinder 的网页上滑动:

https://github.com/dhrubesh/Tinder/blob/master/tinder.js

但现在它可以被视为一个机器人,因为它不断向右滑动,因此我希望有 70/30 的滑动规则并修改以下内容:

var random_boolean = Math.random() < 0.7; //Random boolean added
let run = true, time_step = 1000;
const wait = (n = 1) => new Promise((rs, rj) => run ? setTimeout(rs, n) : rj()),
  click = async (cn, i = 0) => {
    document.getElementsByClassName(cn)[i].click();
    return wait(time_step)
  };
document.onkeydown = (e = window.event) => {
  if (e.key === "a") run = run ? true : explore() || true;
  else if (e.key === "z") console.log(time_step *= 0.9);
  else if (e.key === "x") console.log(time_step *= 1.1);
  else run = false;
};
const explore = async () =>
  click("recCard__info").then(() =>
    Array.from(document.getElementsByClassName("bullet")).reduce((p, e) =>
      p.then(() => {
        e.click();
        return wait(time_step)
      }), Promise.resolve())
  ).then(() => {
    // This if statement below doesn't work
    if(random_boolean){
      click("recsGamepad__button--like")
    }else{
      click("recsGamepad__button--dislike")
    }
    //click("recsGamepad__button--like")  <-- this works if there is no if statement
  }

  ).then(() => explore());
explore();

【问题讨论】:

  • 您的random_boolean 看起来只计算了一次。
  • 我怎样才能让它像一个无限循环一样?
  • var random_boolean = Math.random() &lt; 0.7; 不会神奇地重新计算该值。使用function random_boolean() {return Math.random() &lt; 0.7}if (random_boolean()) 或简单地使用if (Math.random() &lt; 0.7)
  • 使用它仍然无法正常工作:function random_boolean() { return Math.random() &lt; 0.7 }if (random_boolean()){ click("recsGamepad__button--like") }else{ click("recsGamepad__button--dislike") }

标签: javascript random boolean


【解决方案1】:

你需要把赋值放在函数里面,所以每次都会重新计算变量。

).then(() => {
    var random_boolean = Math.random() < 0.7;
    if(random_boolean){
      click("recsGamepad__button--like")
    }else{
      click("recsGamepad__button--dislike")
    }

【讨论】:

    猜你喜欢
    • 2017-10-30
    • 2012-01-15
    • 1970-01-01
    • 2017-09-05
    • 2016-08-13
    • 2015-09-15
    • 1970-01-01
    • 2023-03-28
    相关资源
    最近更新 更多