javascript - How to set rules for math.random function? -
this code below.it randomly generate sequence of 11 characters array. work correctly, need follow rules, not repeat same string, , not repeat l , l' (or) r , r' (or) u , u' (or) b , b' consecutively. please try me.
</head> <body> <script type="text/javascript"> function makeid(){ var text = ""; var possible = new array("r", "r'", "l", "l'", "b", "b'"); for( var i=0; < 11; i++ ) //text += possible.charat(math.floor(math.random() * possible.length)); text += possible[(math.floor(math.random() * possible.length))]; return text; // alert(math.floor(math.random() * possible.length)); } </script> <a href="javascript:makeid()">alert random letter</a> <div> <span id="txt"> </span> </div> </body>
to "set rules" need split individual pairs , use last
variable make sure don't select same random value again.
for rubik's cube scrambler need use like:
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(' ');
Comments
Post a Comment