JavaScript - 随机输出字符

标签 javascript random character output

不知道该怎么做,但我需要输出一组字符 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/

相关文章:

javascript - react 导航6 : Override parent navigator's header configuration from an screen inside child navigator

python - 从元组+一些数字中选择一个随机数

objective-c - PDF:字符代码 -> 字形名称 -> NSString

javascript - 返回 MongoDB 中的格式化值 db.collection.find()

javascript - Meteor:从集合中的文档中检索值

php - MySQL - 从大表中选择随机行

c - 在C中使用rand()生成大量 double 时发生段错误

Ruby - 如果数组中的元素包含某个字符,则返回关联的元素

c++ - C++ 对字 rune 字的顺序有什么保证?

Javascript 获取和更改元素标题