Home Web Front-end JS Tutorial Generate 20-digit random numbers in JS, taking 0-9 as an example, it can also be a-z A-Z_javascript skills

Generate 20-digit random numbers in JS, taking 0-9 as an example, it can also be a-z A-Z_javascript skills

May 16, 2016 pm 04:40 PM
random number

JS code:

function s20(){ 
var data=["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"]; 
for(var j=0;j<500;j++){ //500为想要产生的行数
var result="";
for(var i=0;i<20;i++){ //产生20位就使i<20
r=Math.floor(Math.random()*16); //16为数组里面数据的数量,目的是以此当下标取数组data里的值! 
result+=data[r]; //输出20次随机数的同时,让rrr加20次,就是20位的随机字符串了。 
} 
document.write(result); 
document.write("<br/>"); 
} }
Copy after login

Full html code:

<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title></title> 
<script type="text/javascript"> 
function s20(){ 
var data=["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"]; 
for(var j=0;j<500;j++){ 
var result=""; 
for(var i=0;i<20;i++){ 
r=Math.floor(Math.random()*16);

result+=data[r]; 
} 
document.write(result); 
document.write("<br/>"); 
} } 
</script> 
</head> 
<body> 
<input type="button" onclick="s20()" value="产生随机数"> 
</body> 
</html>
Copy after login
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to create a random number generator in Excel How to create a random number generator in Excel Apr 14, 2023 am 09:46 AM

How to create a random number generator in Excel

Java random number generation performance optimization method Java random number generation performance optimization method Jun 30, 2023 pm 12:25 PM

Java random number generation performance optimization method

In-depth understanding of random number generation methods and applications in numpy In-depth understanding of random number generation methods and applications in numpy Jan 03, 2024 am 08:23 AM

In-depth understanding of random number generation methods and applications in numpy

Generate random numbers using the crypto/rand.Read function from the Go language documentation Generate random numbers using the crypto/rand.Read function from the Go language documentation Nov 04, 2023 pm 03:39 PM

Generate random numbers using the crypto/rand.Read function from the Go language documentation

How to generate random numbers and verification codes using PHP arrays How to generate random numbers and verification codes using PHP arrays Jul 16, 2023 am 08:31 AM

How to generate random numbers and verification codes using PHP arrays

How to generate random numbers in Golang lambda function? How to generate random numbers in Golang lambda function? Jun 05, 2024 pm 12:22 PM

How to generate random numbers in Golang lambda function?

How to avoid generating duplicate random numbers in Golang? How to avoid generating duplicate random numbers in Golang? Jun 01, 2024 pm 04:46 PM

How to avoid generating duplicate random numbers in Golang?

Generate random numbers using Java's Math.random() function Generate random numbers using Java's Math.random() function Jul 24, 2023 am 08:45 AM

Generate random numbers using Java's Math.random() function

See all articles