1.随机产生16进制颜色值
function getRandomColorValue() {
var col = '#'+('00000'+(Math.random()*0x1000000<<0).toString(16)).slice(-6);
return col
}
2.随机产生32位的Uid
我们经常会使用node-uuid这个npm包来产生随机的32位id。在一些Demos中为了方便,我们可以通过下面的方法来产生类似的随机32位字符
const uid = () => Math.random().toString(34).slice(2);
3.随机产生+1或者-1
function getPositiveOrNegative() {
return Math.random() > 0.5 ? 1 : -1
}
// 另一种方法
// Math.round(Math.random()) 将产生 0 或者 1
function getPositiveOrNegative() {
return Math.round(Math.random()) * 2 - 1
}