How to specify range on math.random?

AdamH
<html>
<head>
</head>
<body>
<input type="Textbox" id="pwbx" />

<button OnClick="GetRandom()" type="button">generate</button>
<script>
function getCharacter() {
return Math.floor(Math.random() * 18).toString(18);
}
for( var str="", i=0, l=14+Math.floor(Math.random()*3); i<l; i++) {
str += getCharacter();
}
</script>
</body>
</html>

This gives me a random number under 1, like 0.4567432112 etc.

How would I alter this to give me a string of between 14 and 16 characters, random alphanumerical? So a string of either 14, 15 or 16 characters made up of abcdefgh0123456789?

Thanks

Naeem Shaikh

This will return you a string with random length from 14-16

function getRandomString(min, max) {

      num=  Math.ceil(Math.random()*3)+14;

      var text="";

     var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
            for( var i=0; i < num; i++ )
            {
              text += possible.charAt(Math.floor(Math.random() * possible.length));
            }      
}

also refer this, Generate random string/characters in JavaScript

edit:

    <html>
    <head>
    </head>
    <body>
    <input type="Textbox" id="pwbx" />
    
    <button OnClick="GetRandom()" type="button">generate</button>
    <script>
    function GetRandom(min, max) {

          num=  Math.ceil(Math.random()*3)+14;

          var text="";

         var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
                for( var i=0; i < num; i++ )
                {
                  text += possible.charAt(Math.floor(Math.random() * possible.length));
                }     
    document.getElementById("pwbx").value=text ;

 
    }

    </script>
    </body>
    </html>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

JS: How to exclude a range of values whilst using Math.random()?

From Dev

JS: How to exclude a range of values whilst using Math.random()?

From Dev

How does Math.random() generate random numbers beyond it's "native" range?

From Dev

How to display a specific range of random numbers in java using Math.random

From Dev

Math.random range using the size of an arraylist

From Dev

Java Math.random() How random is it?

From Java

How to specify variables with random in Python

From Dev

How to specify year range in r?

From Dev

How to specify year range in r?

From Dev

How does the distribution of Math.random()*50 + Math.random()*20 compare to Math.random()*70?

From Dev

How to make random float in swift with math operations

From Dev

how to make Math.random round to a number

From Dev

How to Print Random number in a Math Table?

From Dev

Math.random() in Java, how is this possible?

From Dev

Math.round (), How to with a random number?

From Dev

Concept of Math.floor(Math.random() * 5 + 1), what is the true range and why?

From Dev

Concept of Math.floor(Math.random() * 5 + 1), what is the true range and why?

From Dev

How to specify minInstancesPerNode parameter for Random Forest?

From Dev

How to place value at random locations in an array using Math.random?

From Dev

(int)Math.random prints random line of numbers outside of set range for first line

From Dev

How to specify a number range as a type in Idris?

From Dev

How to specify a range and perform a function accordingly in matlab?

From Dev

lpr: How to specify the page range "3 to end"?

From Dev

How to specify range with NSPredicate in Objective C

From Dev

How Do I Further Specify Range?

From Dev

How can I specify a sheet as range in Excel?

From Dev

How to specify a column range according to a formula in Excel?

From Dev

How to specify range to iterate through an array in ansible

From Dev

How to specify hour in cron with range over midnight?

Related Related

  1. 1

    JS: How to exclude a range of values whilst using Math.random()?

  2. 2

    JS: How to exclude a range of values whilst using Math.random()?

  3. 3

    How does Math.random() generate random numbers beyond it's "native" range?

  4. 4

    How to display a specific range of random numbers in java using Math.random

  5. 5

    Math.random range using the size of an arraylist

  6. 6

    Java Math.random() How random is it?

  7. 7

    How to specify variables with random in Python

  8. 8

    How to specify year range in r?

  9. 9

    How to specify year range in r?

  10. 10

    How does the distribution of Math.random()*50 + Math.random()*20 compare to Math.random()*70?

  11. 11

    How to make random float in swift with math operations

  12. 12

    how to make Math.random round to a number

  13. 13

    How to Print Random number in a Math Table?

  14. 14

    Math.random() in Java, how is this possible?

  15. 15

    Math.round (), How to with a random number?

  16. 16

    Concept of Math.floor(Math.random() * 5 + 1), what is the true range and why?

  17. 17

    Concept of Math.floor(Math.random() * 5 + 1), what is the true range and why?

  18. 18

    How to specify minInstancesPerNode parameter for Random Forest?

  19. 19

    How to place value at random locations in an array using Math.random?

  20. 20

    (int)Math.random prints random line of numbers outside of set range for first line

  21. 21

    How to specify a number range as a type in Idris?

  22. 22

    How to specify a range and perform a function accordingly in matlab?

  23. 23

    lpr: How to specify the page range "3 to end"?

  24. 24

    How to specify range with NSPredicate in Objective C

  25. 25

    How Do I Further Specify Range?

  26. 26

    How can I specify a sheet as range in Excel?

  27. 27

    How to specify a column range according to a formula in Excel?

  28. 28

    How to specify range to iterate through an array in ansible

  29. 29

    How to specify hour in cron with range over midnight?

HotTag

Archive