JavaScript Ëæ»ú

Math.random()

Math.random() ·µ»Ø 0£¨°üÀ¨£© ÖÁ 1£¨²»°üÀ¨£© Ö®¼äµÄËæ»úÊý£º

ʵÀý

Math.random();				// ·µ»ØËæ»úÊý

Ç××ÔÊÔÒ»ÊÔ

Math.random() ×ÜÊÇ·µ»ØÐ¡ÓÚ 1 µÄÊý¡£

JavaScript Ëæ»úÕûÊý

Math.random() Óë Math.floor() Ò»ÆðʹÓÃÓÃÓÚ·µ»ØËæ»úÕûÊý¡£

ʵÀý

Math.floor(Math.random() * 10);		// ·µ»Ø 0 ÖÁ 9 Ö®¼äµÄÊý

Ç××ÔÊÔÒ»ÊÔ

ʵÀý

Math.floor(Math.random() * 11);		// ·µ»Ø 0 ÖÁ 10 Ö®¼äµÄÊý

Ç××ÔÊÔÒ»ÊÔ

ʵÀý

Math.floor(Math.random() * 100);	// ·µ»Ø 0 ÖÁ 99 Ö®¼äµÄÊý

Ç××ÔÊÔÒ»ÊÔ

ʵÀý

Math.floor(Math.random() * 101);	// ·µ»Ø 0 ÖÁ 100 Ö®¼äµÄÊý

Ç××ÔÊÔÒ»ÊÔ

ʵÀý

Math.floor(Math.random() * 10) + 1;	// ·µ»Ø 1 ÖÁ 10 Ö®¼äµÄÊý

Ç××ÔÊÔÒ»ÊÔ

ʵÀý

Math.floor(Math.random() * 100) + 1;	// ·µ»Ø 1 ÖÁ 100 Ö®¼äµÄÊý

Ç××ÔÊÔÒ»ÊÔ

Ò»¸öÊʵ±µÄËæ»úº¯Êý

ÕýÈçÄã´ÓÉÏÃæµÄÀý×Ó¿´µ½µÄ£¬´´½¨Ò»¸öËæ»úº¯ÊýÓÃÓÚÉú³ÉËùÓÐËæ»úÕûÊýÊÇÒ»¸öºÃÖ÷Òâ¡£

Õâ¸ö JavaScript º¯ÊýʼÖÕ·µ»Ø½éÓÚ min£¨°üÀ¨£©ºÍ max£¨²»°üÀ¨£©Ö®¼äµÄËæ»úÊý£º

ʵÀý

function getRndInteger(min, max) {
    return Math.floor(Math.random() * (max - min) ) + min;
}

Ç××ÔÊÔÒ»ÊÔ

Õâ¸ö JavaScript º¯ÊýʼÖÕ·µ»Ø½éÓÚ min ºÍ max£¨¶¼°üÀ¨£©Ö®¼äµÄËæ»úÊý£º

ʵÀý

function getRndInteger(min, max) {
    return Math.floor(Math.random() * (max - min + 1) ) + min;
}

Ç××ÔÊÔÒ»ÊÔ

VUE