如何两次生成随机字符串

时间:2021-04-21 11:47:42

标签: javascript

function generate() {
  const notations = [" U", " D", " R", " L", " F", " B"];

  const switches= ["", "\'", "2"]; 

  let array = [];

  let last = '';

  let random = 0;

  for (let i = 0; i < 20; i++) {
      
      do {
         random = Math.floor(Math.random() * notations.length);
      } while (last == notations[random]) 


  last = notations[random];

  let Item = notations[random] + switches[parseInt(Math.random()*switches.length)];

  array.push(Item);
  }

  let scrambles = "";

  for(i=0; i< 20; i++) {
     scrambles += array[i];
  }

document.getElementById("div").innerHTML = scrambles;
}

所以我有生成随机字符串的函数,所以输出将类似于 R U' B2 R2 L F L2 B U2 B U F' L' B2 R' L D2 U' L2 R

并且我想生成两次随机字母,因此输出将类似于

R U' B2 R2 L F L2 B U2 B U F' L' B2 R' L D2 U' L2 R
B R' U' F' D L U2 F' R' B D U F R' L' F U2 D L R

我找到了一个解决方案,就是通过复制这样的代码

function generate() {
  const notations = [" U", " D", " R", " L", " F", " B"];

  const switches= ["", "\'", "2"]; 

  let array = [];

  let last = '';

  let random = 0;

  for (let i = 0; i < 20; i++) {
      
      do {
         random = Math.floor(Math.random() * notations.length);
      } while (last == notations[random]) 


  last = notations[random];

  let Item = notations[random] + switches[parseInt(Math.random()*switches.length)];

  array.push(Item);
  }

  let scrambles = "";

  for(i=0; i< 20; i++) {
     scrambles += array[i];
  }

 const notations2 = new Array(" U", " D", " R", " L", " F", " B");

  const switches2= ["", "\'", "2"]; 

  let array2 = [];

  const last2 = '';

  const random2 = 0;

  for (let i = 0; i < 20; i++) {
      
      do {
         random2 = Math.floor(Math.random() * notations2.length);
      } while (last == notations2[random2]) 


  last2 = notations2[random2];

  let Item2 = notations2[random2] + switches2[parseInt(Math.random()*switches2.length)];

  array.push(Item2);
  }

  let scrambles2 = "";

  for(i=0; i< 20; i++) {
     scrambles2 += array[i];
  }
document.getElementById("div").innerHTML = scrambles + "<br>" + scrambles2;
}

但效率不高,有没有更快更有效的方法来做到这一点?

2 个答案:

答案 0 :(得分:1)

只需让 generate 函数返回打乱码并调用两次即可:

function generate(n) {
  const notations = [" U", " D", " R", " L", " F", " B"];
  const switches = ["", "\'", "2"];

  let last = null;
  let scrambles = "";

  for (let i = 0; i < n; ++i) {
    //subtract 1 when you can't select the same as the last
    let available_notations = notations.length - (last === null ? 0 : 1);

    //one random for all combinations
    let random = Math.floor(Math.random() * available_notations * switches.length);

    let nt = Math.floor(random / switches.length); //notation value
    let sw = Math.floor(random % switches.length); //switch value

    if (last !== null && last <= nt) nt += 1; //add 1 when value bigger than last
    last = nt;

    scrambles += notations[nt] + switches[sw];
  }
  return scrambles;
}

document.getElementById("div").innerHTML = generate(20) + '<br/>' + generate(20);
<div id="div"></div>

答案 1 :(得分:1)

您已经将代码封装在一个函数中,因此只需调用它两次(或任意多次)。

正如其他人已经建议的那样,只需从 scrambles 返回您的 generate() 字符串,然后您可以执行以下操作:

function generateNTimes(n) {
    const scrambles = [];
    for (let i = 0; i < n; i++) {
        scrambles.push(generate());
    }
    document.getElementById("div").innerHTML = scrambles.join('<br>');
}