Hướng dẫn dùng math.randomint JavaScript
The Note: A floating-point, pseudo-random number between 0 (inclusive) and 1 (exclusive). Note that as numbers in JavaScript are IEEE 754 floating point numbers with round-to-nearest-even behavior, the ranges claimed for the functions below (excluding the one for Getting a random number between two valuesThis example returns a random number between the specified values. The returned value is no lower than (and may possibly equal)
Getting a random integer between two valuesThis example returns a random
integer between the specified values. The value is no lower than
Note: It might be tempting to use Getting a random integer between two values, inclusiveWhile the
Specifications
Browser compatibilityBCD tables only load in the browser See alsoNội dung bài viết Đọc thêm tips javascript Tipjs - 5 cách chuyển value sang string trong javascript Math.random() functionTrong JavaScript nếu bạn muốn random
giữa 0 và 1 thì sử dụng console.log(Math.random()) 0.5408145050563944 Nếu bạn muốn random giữa 0 và 10 thì chỉ việc nhân thêm 10 như dưới đây. console.log(Math.random() * 10) Nhưng số ra không đẹp được chính vì vậy sử dụng thêm Math.floor() Math.floor() functionSử dụng console.log(Math.floor(5.95)); // expected output: 5 console.log(Math.floor(5.05)); // expected output: 5 Sử dụng console.log(Math.floor(Math.random() * 10)) Math.ceil() functionSử dụng console.log(Math.ceil(.95)); // expected output: 1 console.log(Math.ceil(4)); // expected output: 4 console.log(Math.ceil(7.004)); // expected output: 8 console.log(Math.ceil(Math.random() * 10)) Math.round() functionSử dụng
console.log(Math.round(0.9)); // expected output: 1 console.log(Math.round(5.95), Math.round(5.5), Math.round(5.05)); // expected output: 6 6 5 console.log(Math.round(Math.random() * 10)) Trên đây là những khái niệm basic để làm nền tảng cho việc lập trình, nếu bạn muốn random từ 4 -> 10 thì làm như thế nào? Hoặc random số thập phân từ 4 -> 10 thì làm thế nào? Hoặc bạn muốn random một list thì làm sao? Hãy xem tiếp Math.randomInt(min, max) JavaScriptGet random một số integer giữa min và max Math.randomInt = function (min, max) { return Math.floor(Math.random() * (max - min)) + min; } Use Click the button! Math.randomDec(min, max, decimals)Get random một num có hai số thập phân giữa min và max Math.randomDec = function (min, max, decimals) { return (Math.random() * (max - min) + min).toFixed(decimals || 2); }; Use Click the button! Math.randomList(list)Get random value trong một array Math.randomList = function (list) { return list[Math.randomInt(0, list.length)]; }; Use Click the button! Code Full(function () { Math.randomInt = function (min, max) { return Math.floor(Math.random() * (max - min)) + min; }; Math.randomDec = function (min, max, decimals) { return (Math.random() * (max - min) + min).toFixed(decimals || 2); }; Math.randomList = function (list) { return list[Math.randomInt(0, list.length)]; }; })(); Đọc thêm tips javascript Tipjs - 5 cách chuyển value sang string trong javascript |