不知道该怎么做,但我需要输出一组字符 x、y 和 z。输出为 3 个字符长,由 x、y 和 z 组成。然而,这些 Angular 色彼此之间的可能性一样大。基本上随机输出字符(来自一组,在本例中为 x、y 和 z)。示例输出 - xyy、zzz、yyz 等。我猜我需要一个函数?
最佳答案
你可以这样做:
var chars = ['x','y','z'];
function show_chars(arr){
var l = arr.length;
return arr[Math.floor(Math.random()*l)]
+ arr[Math.floor(Math.random()*l)]
+ arr[Math.floor(Math.random()*l)]
}
console.log(show_chars(chars));
关于JavaScript - 随机输出字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16429513/