1
<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

AdamH
  • 141
  • 5
  • 16

5 Answers5

3

The range of characters 0123456789abcdefgh would be the octadecimal digits (digits in base 18), so you could do this:

function getCharacter() {
    return Math.floor(Math.random() * 18).toString(18);
}

Now do it 14-16 times:

for( var str="", i=0, l=14+Math.floor(Math.random()*3); i<l; i++) {
    str += getCharacter();
}
Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • thanks for your reply. I altered my post to reflect your changes, I also included the entire code. Upon testing it's not giving me any value in the input box when clicking generate...where am I going wrong? – AdamH Nov 20 '14 at 10:32
2

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>
Community
  • 1
  • 1
Naeem Shaikh
  • 15,331
  • 6
  • 50
  • 88
  • Thanks for the reply...I still seem to be struggling to get the value passed to the – AdamH Nov 20 '14 at 10:38
  • This works perfect, so I'm marking this as correct. Thanks to everyone else who made an effort, it is very much appreciated. :) – AdamH Nov 20 '14 at 10:43
  • this answer is not giving alphanumeric, but only alpha characters, since there are no digits in the `possible`. – nl-x Nov 20 '14 at 10:45
  • nl-x thanx for the suggestion, you can simply add 0-9 in the possible array, and it will work as you want, I mentioned a link in my answer to refer which shows it. – Naeem Shaikh Nov 20 '14 at 10:46
  • Hi, I did notice, and as dumb as I might be, this was pretty easy for me to spot and amend, but thanks for pointing that out :) – AdamH Nov 20 '14 at 10:51
1
function get(str1){
    var arr = str1.split("")
    arr.sort(function(){ return Math.random() - 0.5;})
    var str = arr.join("");
    return str
}
get('abcdefgh0123456789')
Anil kumar
  • 11
  • 3
0

use this

var chars=['a','b','c'];
var number=getRandomInt(14, 16); 
var result=randomString(number, chars)

function getRandomInt(min, max) {
  return Math.floor(Math.random() * (max - min)) + min;
}
function randomString(length, chars) {
    var result = '';
    for (var i = length; i > 0; --i) result += chars[Math.round(Math.random() * (chars.length - 1))];
    return result;
}
ashkufaraz
  • 5,179
  • 6
  • 51
  • 82
  • Read all of the question. – Niet the Dark Absol Nov 20 '14 at 10:24
  • as @NiettheDarkAbsol said read the question, OP dont want 14 15 or 16 as value he want a random string. – Lrrr Nov 20 '14 at 10:26
  • He wants a string of 14 - 16 alphanumeric characters. You have given him a random number between 14 & 16 – kaybee99 Nov 20 '14 at 10:27
  • @ashkufaraz - this doesn't work mate, it now doesn't return any value in the input box, it returns as blank on button click. – AdamH Nov 20 '14 at 10:28
  • please delete your answer after 3 time modification, you still didnt get what question is about! – Lrrr Nov 20 '14 at 10:29
  • @AdamH you should set chars. like this var chars=['a','b','c']; – ashkufaraz Nov 20 '14 at 10:31
  • @ Ali Amiri امیر جان این هم به خاطر ضعف زبان انگلیسی – ashkufaraz Nov 20 '14 at 10:35
  • @ashkufaraz - thanks, but this still seems to pass nothing to – AdamH Nov 20 '14 at 10:40
  • @ashkufaraz البته من اسمم علیه، فامیلیم امیریه :) یه توصیه بهت اینکه اگر دنبال کاری و توی استک اور فلو نوشتی دنبال کاری حداقل یه ایمیلی سایتی چیزی از خودت بذار که بتونن باهات تماس بگیرن :D – Lrrr Nov 20 '14 at 20:20
0

Simply do a random to get your char length. And then iterate that many times getting a random char.

var myString = '';
var myStringLength = 14 + (Math.random() * 2); // characters
for (var i = 0; i<myStringLength; i++) {
    var n= Math.floor(Math.random()*62);
    if(n<10) myString += n; //1-10
    else if(n<36) myString += String.fromCharCode(n+55); //A-Z
    else myString += String.fromCharCode(n+61); //a-z
}
nl-x
  • 11,762
  • 7
  • 33
  • 61