开发者

Javascript - generating a random number of specified length from set of specified values

开发者 https://www.devze.com 2022-12-30 12:15 出处:网络
I wanted to generate a random number 32 c开发者_JS百科haracters in length from a specified set of characters.

I wanted to generate a random number 32 c开发者_JS百科haracters in length from a specified set of characters.

For example:

var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZ";
var string_length = 32;

Which would then return a random number of length 32 using the specified characters.


Something like this?

Check out the jsfiddle. I modified it so you can see the progression as the result grows: http://jsfiddle.net/YuyNL/

var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZ";   // Pool of potential characters
var string_length = 32;                     // Desired length of result
var num_chars = chars.length;               // Total number of characters in pool
var result = '';                      // Container to store result

while(string_length--) {      // Run a loop for a duration equal to the length of the result

  // For each iteration, get a random number from 0 to the size of the
  //   number of characters you're using, use that number to grab the index
  //   of the character stored in 'chars', and add it to the end of the result

    result += chars[ Math.floor( Math.random() * num_chars ) ];  

}

$('body').append(result + '<br>');  // This just appends the result to the 'body'
                                    //    if you're using jQuery


var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZ";
var string_length = 32;
var myrnd = [], pos;

// loop as long as string_length is > 0
while (string_length--) {
    // get a random number between 0 and chars.length - see e.g. http://www.shawnolson.net/a/789/make-javascript-mathrandom-useful.html
    pos = Math.floor(Math.random() * chars.length);
    // add the character from the base string to the array
    myrnd.push(chars.substr(pos, 1));
}

// join the array using '' as the separator, which gives us back a string
myrnd.join(''); // e.g "6DMIG9SP1KDEFB4JK5KWMNSI3UMQSSNT"


// Set up a return variable
var randStr = "";

// Split the chars into individual array elements
chars = chars.split("");

// Until string_length is 0...
while (string_length--)
    // ... select a random character from the array
    randStr += chars[Math.floor(Math.random() * chars.length)];

// return the string
return randStr;


Here is a simple solution that should be quite easy to understand.

var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZ";
var string_length = 32;
var result = "";

for(var i=0; i<string_length; i++){
  var randomPos = Math.floor( Math.random() * chars.length );
  result += chars.substr(randomPos, 1);
}


Maybe make it a little more flexible.

You may want the return to be an array, if you are going to do any big integer math on it

function charsRandom(len, chars){
    chars= chars || "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    len= len || 32;
    var cr= [], L= chars.length;
    while(len--) cr[cr.length]= chars.charAt(Math.floor(Math.random()*L));
    return cr.join(''); // or return the array cr
}

alert(charsRandom())

0

精彩评论

暂无评论...
验证码 换一张
取 消