• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Generate random string via Javascript

New Here ,
Sep 26, 2017 Sep 26, 2017

Copy link to clipboard

Copied

Hello all!

I am not good with Javascript but was able to come up with random numbers using these forums and Google.

Does anyone know how to come up with a random string?  IE 6 to 8 characters of numbers and letters?  I have seen some Javascript that seems to work but not sure if getting it to work in Captivate is different as I have not had success.

This is what I used for number:

function getRandomInt(min, max) {

   var jsRandomNumber = Math.floor(Math.random() * (max - min + 1)) + min;

   alert(jsRandomNumber);

}

getRandomInt(0,1000);

Appreciate the time!

Views

2.0K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 15, 2017 Dec 15, 2017

Copy link to clipboard

Copied

You could try this:

function randomString() {

var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";

var string_length = 8;

var randomstring = '';

for (var i=0; i<string_length; i++) {

var rnum = Math.floor(Math.random() * chars.length);

randomstring += chars.substring(rnum,rnum+1);

}

document.randform.randomfield.value = randomstring;

}

Then you can put in a form with "onkeyup="randomString();"" or a button with "onclick="randomString();"".

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 09, 2018 May 09, 2018

Copy link to clipboard

Copied

Hello!

You should use, for example:

function getRandomString(l) {   

    var chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';

    var charsLength = chars.length;

    var string = '';

     for(var i=0; i<l; i++) {

        string += chars.charAt(Math.floor(Math.random() * charsLength));

    }

   return string;

}

more variants how to generate random string in js: http://javascript-benchmark.info/t/generate-random-string

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 15, 2022 Oct 15, 2022

Copy link to clipboard

Copied

LATEST

We can simply use short JS code as below for 6 characters string

let random = (Math.random() + 1).toString(36).substring(6);
console.log(random);

You can take a look at more ways here:

https://qawithexperts.com/questions/242/how-to-generate-random-string-using-javascript

Thanks.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Help resources