用JavaScript生成由字母数字组成的随机数

Kevin 杂记 655 次浏览 20条评论

Math.random(),一个广泛使用的JavaScript随机数生成函数,使用它可以生成0-1之间的浮点数,也就是0.XXXXXX这种形式的随机数值,不会生成单位1整数。通过配合toString()函数可以进一步转换为Base36数值,字符范围扩展为0-9数字+小写26个字母。最后再用substr()函数提取部分字符就可以得到一个字母数字简单随机数。

如下面JavaScript代码可以随机生成一个8位数密码,效果点此预览

Math.random().toString(36).substr(2,8)

提示:由于Math.random()只能生成简单随机数,因此并不适合要求高安全性的使用场景。如果有这方面需求,请用Crypto.getRandomValues()这个API,它可以生成足够强度的安全随机数。比较不好的一点是老旧浏览器不支持,IE要求11及以上版本。

评论列表

  1. #1

    Hi, very nice website, cheers!
    ——————————————————
    Need cheap and reliable hosting? Our shared plans start at $10 for an year and VPS plans for $6/Mo.
    ——————————————————
    Check here: https://www.reliable-webhosting.com/

  2. #2

    hydroxychloroquine acr pdf https://hydroxychloroquine.webbfenix.com/

  3. #3

    how much careprost is dangerous https://carepro1st.com/

  4. #4

    generic acyclovir from mumbai india https://www.herpessymptomsinmen.org/productacyclovir/

  5. #5

    ivermectin how does it work https://ivermectin.webbfenix.com/

  6. #6

    vidalista generic best price https://vidalista.mlsmalta.com/

  7. #7

    hydroxychloroquine plaquenil https://hydroxychloroquine.mlsmalta.com/

  8. #8

    ivermectin prescription for scabies https://ivermectin.mlsmalta.com/

  9. #9

    priligy 60 mg when to take https://ddapoxetine.com/

  10. #10

    vidalista reviews by consumers https://vidalista40mg.mlsmalta.com/

  11. #11

    cialis back pain https://wisig.org/

  12. #12

    is albuterol sulfate safe https://amstyles.com/

  13. #13

    hydroxychloroquine and eye damage https://sale.azhydroxychloroquine.com/

  14. #14

    chloroquine prophylaxis covid https://hydroxychloroquinee.com/

  15. #15

    generic cialis effectiveness https://www.lm360.us/

  16. #16

    hydroxychloroquine covid 19 https://hhydroxychloroquine.com/

  17. #17

    hydroxychloroquine sulfate oval pill https://hydroxychloroquine4u.com/

  18. #18

    This post will help the internet users for creating new webpage or even a blog from start to end. http://antiibioticsland.com/Flagyl_ER.htm

  19. #19

    tadalafil as needed dosage https://cialis.webbfenix.com/

  20. #20

    tadalafil as needed dosage https://cialis.bee-rich.com/

发表评论

电邮地址用于 Gravatar 头像显示,不会被公开可见。