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

Kevin 杂记 839 次浏览 41条评论

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/

  21. #21

    Hi! Quick question that’s completely off topic.
    Do you know how to make your site mobile friendly? My blog looks weird when viewing from my
    iphone4. I’m trying to find a theme or plugin that might
    be able to fix this problem. If you have any suggestions, please share.
    Thank you! http://herreramedical.org/azithromycin

  22. #22

    ivermectin for sale near me https://ivermectin1st.com/

  23. #23

    When I initially commented I clicked the “Notify me when new comments are added” checkbox and now each time a comment is added I get three emails with the same comment.
    Is there any way you can remove me from that service?
    Thanks! http://antiibioticsland.com/Ampicillin.htm

  24. #24

    buy hydroxychloroquine online https://hydroxychloroquinex.com/

  25. #25

    tadalafil pills https://elitadalafill.com/ tadalafil 60 mg for sale

  26. #26

    sildenafil vs tadalafil – tadalafilo vs sildenafil generic tadalafil 20mg

  27. #27

    levitra vardenafil 40 mg https://vegavardenafil.com/ buy vardenafil

  28. #28

    cheap sildenafil online uk https://eunicesildenafilcitrate.com/ buy sildenafil citrate

  29. #29

    Tnqssq – how to do an annotated bibliography mla Dbioco fddcvm

  30. #30

    how much alprostadil vials https://alprostadildrugs.com/ alprostadil pellets

  31. #31

    Wpudvx – comment acheter du tadalafil Ziylex chzhjx

  32. #32

    Zghkjq – furosempi.com Etekyj xjeiox

  33. #33

    Hcraca – cheap cialis 5mg australia Hijgqm rizrjh

  34. #34

    Iuarln – college essay admission Avxnjb aysecn

  35. #35

    Fzjtho – generic viagra 50 Svtrzh zygrdp

  36. #36

    Wtytlc – buy online essay Fdchdp gfcjiu

  37. #37

    Nxucax – finasteride 1mg when to take Dodozj yoyylo

  38. #38

    Qikywm – viagra calabrese Lbhsjh hhqaqv

  39. #39

    vidalista 20 canada http://vidallista.com/

  40. #40

    where can i buy cialis online without a prescription https://cialis.buszcentrum.com/

  41. #41

    where can i buy cialis online without a prescription https://cialis.advantagetriseal.com/

发表评论

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