javascript - 如何获得随机数而不是随机数和字母?
问题描述
我想制作随机的三位数,但是当我运行它时,它会生成字母数字字符。我只想得到一个随机的三位数。
产生:
function () {
this.newItem.st = Math.random().toString(36).substring(2, 3).toUpperCase() +
Math.random().toString(36).substring(2, 4).toUpperCase()
}
当我单击生成时,我只想获得 0-9 而不是 AZ 的随机值。
解决方案
将你的随机数乘以 10 得到一个位置,使用 Math.floor 得到一个整数,然后转换为字符串得到一串数字:
const el = document.getElementById("output");
el.innerHTML = getNumber();
function getNumber() {
let myNum = Math.floor(Math.random() * 10).toString() + Math.floor(Math.random() * 10).toString() + Math.floor(Math.random() * 10).toString();
return myNum;
}
推荐阅读
- css - 如何更改占位符字体
- android - 如何设置两列recyclerview?
- gwt - 从外部窗口插入新元素后如何重新呈现手风琴布局
- python - Pycharm 运行 geopandas 模块中的错误消息。我该如何解决?
- android - Android Studio 生成的代码未显示正确命名的变量
- ios - 由于动态链接依赖关系,使用 Firebase Messaging for iOS 构建 Unity 失败
- arrays - TypeError:无法读取未定义的属性“FuId”
- java - 实现 JpaRepository 的最佳实践
- python - 在 Python 中计算联邦税总额
- node.js - NodeJS - AJAX POST 404(未找到)