Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

生成随机数字串的正确姿势 #4

Open
AymaxLi opened this issue Jul 16, 2018 · 0 comments
Open

生成随机数字串的正确姿势 #4

AymaxLi opened this issue Jul 16, 2018 · 0 comments
Assignees

Comments

@AymaxLi
Copy link
Owner

AymaxLi commented Jul 16, 2018

错误姿势

生成长度为 4 的随机数字串

String.prototype.slice.call(Math.random() * 10000, 0, 4)

之前在生成数字串时用过这个方法,后来发现会出现 453. 这样错误数字串,因为 Math.random() 生成了 0.0453xxxxxxxxxx 这样的随机数,这个数字乘以 10000 就变成 453.xxxxxxx,前面的零被吃掉了。。。

正确姿势

所以正确姿势应该是介个样几

String.prototype.slice.call(Math.random(), 2, 6)

计划通 ~

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant