JavaScript - 随机词生成器

her*_*xie 1 javascript math

很好奇为什么math.floormath.ceil我做随机词生成和签入时返回更好的结果console.log

为什么不能math.ceil完美运行?是否存在math.ceil与与不兼容的问题,math.random或者是我如何分配字符串元素的数组编号(项目)?

Den*_*er1 6

Math.random() 返回一个小于 1(非包含)但大于 0(包含)的数字

Math.floor(Math.random() * 10); // returns a random integer from 0 to 9

Math.ceil(Math.random() * 10); // returns a random integer from 0 to 10 with a very low chance of 0
Run Code Online (Sandbox Code Playgroud)

如果Math.random结果0完全相同,则Math.floor()andMath.ceil()都将返回0,但如果Math.random()结果为0.00000001,则Math.floor()返回0Math.ceil()返回1