Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/394.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何为math.random函数设置规则?_Javascript_Html - Fatal编程技术网

Javascript 如何为math.random函数设置规则?

Javascript 如何为math.random函数设置规则?,javascript,html,Javascript,Html,这是我下面的代码。它将从数组中随机生成11个字符的序列。它正确地工作,但我需要它遵循一些规则,比如不重复相同的字符串,不连续重复L和L'(或)R和R'(或)U和U'(或)B和B'。请试着帮助我。 函数makeid(){ var text=“”; var可能=新数组(“R”、“R'”、“L”、“L'”、“B”、“B'”); 对于(变量i=0;i

这是我下面的代码。它将从数组中随机生成11个字符的序列。它正确地工作,但我需要它遵循一些规则,比如不重复相同的字符串,不连续重复L和L'(或)R和R'(或)U和U'(或)B和B'。请试着帮助我。


函数makeid(){
var text=“”;
var可能=新数组(“R”、“R'”、“L”、“L'”、“B”、“B'”);
对于(变量i=0;i<11;i++)
//text+=可能的.charAt(Math.floor(Math.random()*可能的.length));
text+=可能[(Math.floor(Math.random()*可能的.length))];
返回文本;
//警报(Math.floor(Math.random()*可能的.length));
}
要“设置规则”,您需要拆分各个对并使用
last
变量,以确保不再选择相同的随机值

对于您的魔方扰频器,您需要使用以下内容:

var out = [], last = -1, p = [["R", "R'"], ["L", "L'"], ["B", "B'"], ["U", "U'"], ["F", "F'"], ["D", "D'"]], num = 11;
while(out.length !== num){
    rand = Math.floor(Math.random() * p.length);
    if(rand !== last){
        last = rand;
        out.push(p[rand][Math.floor(Math.random() * p[rand].length)]);
    }
}
return out.join(' ');

你不能“设定规则”。如果您不想重复,您必须跟踪并自己进行检查。实现这一点的最简单方法是制作一个检查器函数,根据该函数的输出验证您的规则,如果不确定,则重新滚动并在返回前重试。确保永远不会产生无效序列会更有效(但更复杂)让我猜这是rubiks多维数据集加扰器生成器的功能?两种解决方案:以某种方式将
[0,1)
转换为仅有效的值空间(而不是从整个值空间中进行选择);或者反复尝试,直到找到有效结果。是的!!这是用于rubiks多维数据集置乱生成器的
var out = [], last = -1, p = [["R", "R'"], ["L", "L'"], ["B", "B'"], ["U", "U'"], ["F", "F'"], ["D", "D'"]], num = 11;
while(out.length !== num){
    rand = Math.floor(Math.random() * p.length);
    if(rand !== last){
        last = rand;
        out.push(p[rand][Math.floor(Math.random() * p[rand].length)]);
    }
}
return out.join(' ');