JS implements a method for randomly generating strings (length can be specified)

1. Define a random interception string

 const _charStr = 'abacdefghjklmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789';

2. Define the randomly generated index function

 /** * 随机生成索引* @param min 最小值* @param max 最大值* @param i 当前获取位置*/ function RandomIndex(min, max, i){ let index = Math.floor(Math.random()*(max-min+1)+min), numStart = _charStr.length - 10; //如果字符串第一位是数字,则递归重新获取if(i==0&&index>=numStart){ index = RandomIndex(min,

The post JS implements a method to randomly generate a string (with a specified length) first appeared on Lenix Blog .

This article is reprinted from https://blog.p2hp.com/archives/9065
This site is for inclusion only, and the copyright belongs to the original author.

Leave a Comment