Saat bekerja dengan program JavaScript, mungkin ada saatnya Anda perlu membuat nomor acak
Misalnya, Anda mungkin ingin menghasilkan angka acak saat mengembangkan game JavaScript, seperti game tebak angka
JavaScript memiliki banyak metode bawaan untuk bekerja dengan angka dan melakukan perhitungan matematis. Salah satu metode tersebut adalah metode console.log(Math.round(6.2)); // 6 console.log(Math.round(6.3)); // 6 console.log(Math.round(6.5)); // 7 console.log(Math.round(6.8)); // 7 7
Pada artikel ini, Anda akan mempelajari cara menggunakan metode console.log(Math.round(6.2)); // 6 console.log(Math.round(6.3)); // 6 console.log(Math.round(6.5)); // 7 console.log(Math.round(6.8)); // 7 7 untuk mengambil nomor acak
Inilah yang akan kami bahas
Cara Menggunakan Matematika di JavaScript - Pengantar Objek console.log(Math.round(6.2)); // 6 console.log(Math.round(6.3)); // 6 console.log(Math.round(6.5)); // 7 console.log(Math.round(6.8)); // 7 9
JavaScript memiliki objek statis bawaan console.log(Math.round(6.2)); // 6 console.log(Math.round(6.3)); // 6 console.log(Math.round(6.5)); // 7 console.log(Math.round(6.8)); // 7 _9, yang memungkinkan Anda melakukan perhitungan dan operasi matematis
Objek console.log(Math.round(6.2)); // 6 console.log(Math.round(6.3)); // 6 console.log(Math.round(6.5)); // 7 console.log(Math.round(6.8)); // 7 9 memiliki beberapa metode bawaan yang membuat pelaksanaan operasi tersebut jauh lebih akurat
Sintaks umum untuk metode objek ________0______9 adalah sebagai berikut
Math.methodName(number); _Beberapa metode yang paling populer adalah
- console.log(Math.floor(6.2)); // 6 console.log(Math.floor(6.3)); // 6 console.log(Math.floor(6.5)); // 6 console.log(Math.floor(6.8)); // 6 2
- console.log(Math.floor(6.2)); // 6 console.log(Math.floor(6.3)); // 6 console.log(Math.floor(6.5)); // 6 console.log(Math.floor(6.8)); // 6 3
- console.log(Math.floor(6.2)); // 6 console.log(Math.floor(6.3)); // 6 console.log(Math.floor(6.5)); // 6 console.log(Math.floor(6.8)); // 6 4
- console.log(Math.round(6.2)); // 6 console.log(Math.round(6.3)); // 6 console.log(Math.round(6.5)); // 7 console.log(Math.round(6.8)); // 7 _7
Mari kita lihat beberapa contoh bagaimana mengimplementasikan metode tersebut
Jika Anda ingin membulatkan angka ke bilangan bulat terdekat, gunakan metode console.log(Math.floor(6.2)); // 6 console.log(Math.floor(6.3)); // 6 console.log(Math.floor(6.5)); // 6 console.log(Math.floor(6.8)); // 6 2
console.log(Math.round(6.2)); // 6 console.log(Math.round(6.3)); // 6 console.log(Math.round(6.5)); // 7 console.log(Math.round(6.8)); // 7Jika Anda ingin membulatkan angka ke bilangan bulat terdekat, gunakan metode console.log(Math.floor(6.2)); // 6 console.log(Math.floor(6.3)); // 6 console.log(Math.floor(6.5)); // 6 console.log(Math.floor(6.8)); // 6 3
console.log(Math.ceil(6.2)); // 7 console.log(Math.ceil(6.3)); // 7 console.log(Math.ceil(6.5)); // 7 console.log(Math.ceil(6.8)); // 7Jika Anda ingin membulatkan angka ke bilangan bulat terdekat, gunakan metode console.log(Math.floor(6.2)); // 6 console.log(Math.floor(6.3)); // 6 console.log(Math.floor(6.5)); // 6 console.log(Math.floor(6.8)); // 6 4
console.log(Math.floor(6.2)); // 6 console.log(Math.floor(6.3)); // 6 console.log(Math.floor(6.5)); // 6 console.log(Math.floor(6.8)); // 6Jika Anda ingin menghasilkan nomor acak, gunakan metode console.log(Math.round(6.2)); // 6 console.log(Math.round(6.3)); // 6 console.log(Math.round(6.5)); // 7 console.log(Math.round(6.8)); // 7 7
console.log(Math.random()); // You will not get the same output //first time running the program: // 0.4928793139100267 //second time running the program: // 0.5420802533292215 // third time running the program: // 0.5479835477696466Apa Metode console.log(Math.round(6.2)); // 6 console.log(Math.round(6.3)); // 6 console.log(Math.round(6.5)); // 7 console.log(Math.round(6.8)); // 7 _7 dalam JavaScript?
Sintaks untuk metode console.log(Math.round(6.2)); // 6 console.log(Math.round(6.3)); // 6 console.log(Math.round(6.5)); // 7 console.log(Math.round(6.8)); // 7 7 adalah sebagai berikut
Math.random();Metode ini tidak mengambil parameter apa pun
Secara default, metode mengembalikan nilai desimal acak (atau floating point) angka antara console.log(Math.random()); // You will not get the same output //first time running the program: // 0.4928793139100267 //second time running the program: // 0.5420802533292215 // third time running the program: // 0.5479835477696466 2 dan console.log(Math.random()); // You will not get the same output //first time running the program: // 0.4928793139100267 //second time running the program: // 0.5420802533292215 // third time running the program: // 0.5479835477696466 3
Sesuatu yang perlu diperhatikan tentang ini adalah bahwa console.log(Math.random()); // You will not get the same output //first time running the program: // 0.4928793139100267 //second time running the program: // 0.5420802533292215 // third time running the program: // 0.5479835477696466 _2 inklusif, sedangkan console.log(Math.random()); // You will not get the same output //first time running the program: // 0.4928793139100267 //second time running the program: // 0.5420802533292215 // third time running the program: // 0.5479835477696466 3 tidak inklusif
Jadi, itu akan mengembalikan nilai lebih besar dari atau sama dengan console.log(Math.random()); // You will not get the same output //first time running the program: // 0.4928793139100267 //second time running the program: // 0.5420802533292215 // third time running the program: // 0.5479835477696466 2 dan selalu kurang dari dan tidak pernah sama dengan console.log(Math.random()); // You will not get the same output //first time running the program: // 0.4928793139100267 //second time running the program: // 0.5420802533292215 // third time running the program: // 0.5479835477696466 3
Cara Menghasilkan Desimal Acak Dengan Batas console.log(Math.ceil(6.2)); // 7 console.log(Math.ceil(6.3)); // 7 console.log(Math.ceil(6.5)); // 7 console.log(Math.ceil(6.8)); // 7 1 Tertentu Menggunakan console.log(Math.round(6.2)); // 6 console.log(Math.round(6.3)); // 6 console.log(Math.round(6.5)); // 7 console.log(Math.round(6.8)); // 7 7 dalam JavaScript
Seperti yang telah Anda lihat sejauh ini, secara default, angka console.log(Math.round(6.2)); // 6 console.log(Math.round(6.3)); // 6 console.log(Math.round(6.5)); // 7 console.log(Math.round(6.8)); // 7 7 dihasilkan kecil
Bagaimana jika Anda ingin menghasilkan angka desimal acak yang dimulai dari, dan mencakup, console.log(Math.random()); // You will not get the same output //first time running the program: // 0.4928793139100267 //second time running the program: // 0.5420802533292215 // third time running the program: // 0.5479835477696466 2 dan juga lebih besar dari console.log(Math.random()); // You will not get the same output //first time running the program: // 0.4928793139100267 //second time running the program: // 0.5420802533292215 // third time running the program: // 0.5479835477696466 3?
Secara khusus, Anda perlu mengalikan angka console.log(Math.ceil(6.2)); // 7 console.log(Math.ceil(6.3)); // 7 console.log(Math.ceil(6.5)); // 7 console.log(Math.ceil(6.8)); // 7 1 ini dengan angka acak dari console.log(Math.round(6.2)); // 6 console.log(Math.round(6.3)); // 6 console.log(Math.round(6.5)); // 7 console.log(Math.round(6.8)); // 7 7
Misalnya, jika Anda ingin menghasilkan angka acak antara console.log(Math.random()); // You will not get the same output //first time running the program: // 0.4928793139100267 //second time running the program: // 0.5420802533292215 // third time running the program: // 0.5479835477696466 2 dan Math.random(); 6 Anda akan melakukan hal berikut
console.log(Math.random() * 10); // You won't get the same output //9.495628210218175Dalam contoh di atas, saya mengalikan angka console.log(Math.ceil(6.2)); // 7 console.log(Math.ceil(6.3)); // 7 console.log(Math.ceil(6.5)); // 7 console.log(Math.ceil(6.8)); // 7 1 (Math.random(); 6), yang akan bertindak sebagai limit, dengan angka yang dihasilkan dari console.log(Math.round(6.2)); // 6 console.log(Math.round(6.3)); // 6 console.log(Math.round(6.5)); // 7 console.log(Math.round(6.8)); // 7 7
Ingatlah bahwa dalam kasus ini, angka acak akan berada di antara console.log(Math.random()); // You will not get the same output //first time running the program: // 0.4928793139100267 //second time running the program: // 0.5420802533292215 // third time running the program: // 0.5479835477696466 2 dan Math.random(); 6 - ini berarti lebih besar dari atau sama dengan console.log(Math.random()); // You will not get the same output //first time running the program: // 0.4928793139100267 //second time running the program: // 0.5420802533292215 // third time running the program: // 0.5479835477696466 2 dan kurang dari dan tidak pernah sama dengan Math.random(); 6
Cara Menghasilkan Desimal Acak Dengan Rentang console.log(Math.ceil(6.2)); // 7 console.log(Math.ceil(6.3)); // 7 console.log(Math.ceil(6.5)); // 7 console.log(Math.ceil(6.8)); // 7 2 dan console.log(Math.ceil(6.2)); // 7 console.log(Math.ceil(6.3)); // 7 console.log(Math.ceil(6.5)); // 7 console.log(Math.ceil(6.8)); // 7 1 Tertentu Menggunakan console.log(Math.round(6.2)); // 6 console.log(Math.round(6.3)); // 6 console.log(Math.round(6.5)); // 7 console.log(Math.round(6.8)); // 7 7 dalam JavaScript
Bagaimana jika Anda ingin menghasilkan angka desimal acak di antara rentang angka yang Anda tentukan?
Anda melihat di bagian sebelumnya cara menentukan console.log(Math.ceil(6.2)); // 7 console.log(Math.ceil(6.3)); // 7 console.log(Math.ceil(6.5)); // 7 console.log(Math.ceil(6.8)); // 7 1, tetapi bagaimana jika Anda tidak ingin rentang dimulai dari console.log(Math.random()); // You will not get the same output //first time running the program: // 0.4928793139100267 //second time running the program: // 0.5420802533292215 // third time running the program: // 0.5479835477696466 2 (yang merupakan rentang awal default)?
Sintaks umum untuk menghasilkan desimal antara dua nilai (atau rentang) terlihat mirip dengan ini
Math.random() * (max - min) + min;Mari kita ambil contoh berikut
// specify a minimum - where the range will start let min = 20.4; // specify a maximum - where the range will end let max = 29.8;Saya membuat dua variabel, console.log(Math.ceil(6.2)); // 7 console.log(Math.ceil(6.3)); // 7 console.log(Math.ceil(6.5)); // 7 console.log(Math.ceil(6.8)); // 7 _2 dan console.log(Math.ceil(6.2)); // 7 console.log(Math.ceil(6.3)); // 7 console.log(Math.ceil(6.5)); // 7 console.log(Math.ceil(6.8)); // 7 1 - di mana console.log(Math.ceil(6.2)); // 7 console.log(Math.ceil(6.3)); // 7 console.log(Math.ceil(6.5)); // 7 console.log(Math.ceil(6.8)); // 7 2 akan menjadi angka terkecil dan console.log(Math.ceil(6.2)); // 7 console.log(Math.ceil(6.3)); // 7 console.log(Math.ceil(6.5)); // 7 console.log(Math.ceil(6.8)); // 7 1 angka terbesar dalam rentang
Selanjutnya, saya menghasilkan nomor acak dalam rentang itu menggunakan sintaks yang Anda lihat sebelumnya
let min = 20.4; let max = 29.8; let randomNum = Math.random() * (max - min) + min; console.log(randomNum); // You won't get the same output // 23.309418058783486Sesuatu yang perlu diperhatikan di sini adalah bahwa console.log(Math.ceil(6.2)); // 7 console.log(Math.ceil(6.3)); // 7 console.log(Math.ceil(6.5)); // 7 console.log(Math.ceil(6.8)); // 7 _2 inklusif, sehingga angka acak akan lebih besar dari atau sama dengan Math.random() * (max - min) + min; 5, sedangkan console.log(Math.ceil(6.2)); // 7 console.log(Math.ceil(6.3)); // 7 console.log(Math.ceil(6.5)); // 7 console.log(Math.ceil(6.8)); // 7 1 tidak inklusif, sehingga hasilnya akan selalu berupa angka yang kurang dari dan tidak pernah sama dengan Math.random() * (max - min) + min; 7
Cara Menghasilkan Bilangan Bulat Acak Dengan Batas console.log(Math.ceil(6.2)); // 7 console.log(Math.ceil(6.3)); // 7 console.log(Math.ceil(6.5)); // 7 console.log(Math.ceil(6.8)); // 7 1 Tertentu Menggunakan console.log(Math.round(6.2)); // 6 console.log(Math.round(6.3)); // 6 console.log(Math.round(6.5)); // 7 console.log(Math.round(6.8)); // 7 7 dalam JavaScript
Sejauh ini, Anda telah melihat cara menghasilkan angka desimal acak
Yang mengatakan, ada cara untuk menghasilkan bilangan bulat acak menggunakan console.log(Math.round(6.2)); // 6 console.log(Math.round(6.3)); // 6 console.log(Math.round(6.5)); // 7 console.log(Math.round(6.8)); // 7 7
Anda harus meneruskan hasil perhitungan console.log(Math.round(6.2)); // 6 console.log(Math.round(6.3)); // 6 console.log(Math.round(6.5)); // 7 console.log(Math.round(6.8)); // 7 7 ke metode console.log(Math.floor(6.2)); // 6 console.log(Math.floor(6.3)); // 6 console.log(Math.floor(6.5)); // 6 console.log(Math.floor(6.8)); // 6 4 yang Anda lihat sebelumnya
Sintaks umum untuk ini adalah sebagai berikut
console.log(Math.round(6.2)); // 6 console.log(Math.round(6.3)); // 6 console.log(Math.round(6.5)); // 7 console.log(Math.round(6.8)); // 7 0Metode console.log(Math.floor(6.2)); // 6 console.log(Math.floor(6.3)); // 6 console.log(Math.floor(6.5)); // 6 console.log(Math.floor(6.8)); // 6 _4 akan membulatkan bilangan desimal acak ke bilangan bulat (atau bilangan bulat) terdekat
Selain itu, Anda dapat menentukan nomor console.log(Math.ceil(6.2)); // 7 console.log(Math.ceil(6.3)); // 7 console.log(Math.ceil(6.5)); // 7 console.log(Math.ceil(6.8)); // 7 1
Jadi, misalnya, jika Anda ingin menghasilkan angka acak antara console.log(Math.random()); // You will not get the same output //first time running the program: // 0.4928793139100267 //second time running the program: // 0.5420802533292215 // third time running the program: // 0.5479835477696466 2 dan Math.random(); 6, Anda akan melakukan hal berikut
console.log(Math.round(6.2)); // 6 console.log(Math.round(6.3)); // 6 console.log(Math.round(6.5)); // 7 console.log(Math.round(6.8)); // 7 _1Dalam contoh ini, saya mengalikan angka console.log(Math.ceil(6.2)); // 7 console.log(Math.ceil(6.3)); // 7 console.log(Math.ceil(6.5)); // 7 console.log(Math.ceil(6.8)); // 7 1 (yaitu Math.random(); 6 dalam hal ini) dengan hasil dari console.log(Math.round(6.2)); // 6 console.log(Math.round(6.3)); // 6 console.log(Math.round(6.5)); // 7 console.log(Math.round(6.8)); // 7 7 dan meneruskan hasil perhitungan ini ke console.log(Math.floor(6.2)); // 6 console.log(Math.floor(6.3)); // 6 console.log(Math.floor(6.5)); // 6 console.log(Math.floor(6.8)); // 6 4
Sesuatu yang perlu diperhatikan di sini adalah bahwa nomor acak akan berada di antara console.log(Math.random()); // You will not get the same output //first time running the program: // 0.4928793139100267 //second time running the program: // 0.5420802533292215 // third time running the program: // 0.5479835477696466 2 inklusif dan Math.random(); 6 eksklusif. Jadi, jumlahnya bisa lebih besar dari atau sama dengan console.log(Math.random()); // You will not get the same output //first time running the program: // 0.4928793139100267 //second time running the program: // 0.5420802533292215 // third time running the program: // 0.5479835477696466 2 dan kurang dari dan tidak pernah sama dengan Math.random(); 6
Cara Menghasilkan Bilangan Bulat Acak Dengan console.log(Math.ceil(6.2)); // 7 console.log(Math.ceil(6.3)); // 7 console.log(Math.ceil(6.5)); // 7 console.log(Math.ceil(6.8)); // 7 1 Tertentu Menggunakan Batas Inklusif Menggunakan console.log(Math.round(6.2)); // 6 console.log(Math.round(6.3)); // 6 console.log(Math.round(6.5)); // 7 console.log(Math.round(6.8)); // 7 7 dalam JavaScript
Pada contoh dari bagian sebelumnya, saya menghasilkan angka acak antara angka console.log(Math.random()); // You will not get the same output //first time running the program: // 0.4928793139100267 //second time running the program: // 0.5420802533292215 // third time running the program: // 0.5479835477696466 2 (inklusif) dan Math.random(); 6 (eksklusif)
Sejauh ini, Anda telah melihat bahwa Anda tidak dapat menghasilkan angka acak yang sama dengan console.log(Math.ceil(6.2)); // 7 console.log(Math.ceil(6.3)); // 7 console.log(Math.ceil(6.5)); // 7 console.log(Math.ceil(6.8)); // 7 1 yang ditentukan
Bagaimana jika Anda ingin menghasilkan angka acak yang menyertakan maks yang ditentukan?
Solusi untuk ini adalah dengan menambahkan console.log(Math.random()); // You will not get the same output //first time running the program: // 0.4928793139100267 //second time running the program: // 0.5420802533292215 // third time running the program: // 0.5479835477696466 3 selama perhitungan
Jadi, mari kita lihat kembali kode dari bagian sebelumnya
console.log(Math.round(6.2)); // 6 console.log(Math.round(6.3)); // 6 console.log(Math.round(6.5)); // 7 console.log(Math.round(6.8)); // 7 _2Anda sekarang akan menulis ulang kode seperti itu
console.log(Math.round(6.2)); // 6 console.log(Math.round(6.3)); // 6 console.log(Math.round(6.5)); // 7 console.log(Math.round(6.8)); // 7 _3Sesuatu yang perlu diperhatikan di sini adalah bahwa kode ini akan menghasilkan bilangan bulat acak antara console.log(Math.random()); // You will not get the same output //first time running the program: // 0.4928793139100267 //second time running the program: // 0.5420802533292215 // third time running the program: // 0.5479835477696466 3(bukan console.log(Math.random()); // You will not get the same output //first time running the program: // 0.4928793139100267 //second time running the program: // 0.5420802533292215 // third time running the program: // 0.5479835477696466 2) dan Math.random(); 6 - termasuk Math.random(); 6
Cara Menghasilkan Bilangan Bulat Acak Dengan console.log(Math.ceil(6.2)); // 7 console.log(Math.ceil(6.3)); // 7 console.log(Math.ceil(6.5)); // 7 console.log(Math.ceil(6.8)); // 7 2 Tertentu dan console.log(Math.ceil(6.2)); // 7 console.log(Math.ceil(6.3)); // 7 console.log(Math.ceil(6.5)); // 7 console.log(Math.ceil(6.8)); // 7 1 Rentang Inklusif Menggunakan console.log(Math.round(6.2)); // 6 console.log(Math.round(6.3)); // 6 console.log(Math.round(6.5)); // 7 console.log(Math.round(6.8)); // 7 7 dalam JavaScript
Sejauh ini, Anda telah melihat cara membuat bilangan bulat acak dengan console.log(Math.ceil(6.2)); // 7 console.log(Math.ceil(6.3)); // 7 console.log(Math.ceil(6.5)); // 7 console.log(Math.ceil(6.8)); // 7 1 inklusif yang Anda tentukan
Seperti yang Anda lihat pada contoh di atas, kode untuk membuat angka console.log(Math.ceil(6.2)); // 7 console.log(Math.ceil(6.3)); // 7 console.log(Math.ceil(6.5)); // 7 console.log(Math.ceil(6.8)); // 7 1 menggunakan angka console.log(Math.random()); // You will not get the same output //first time running the program: // 0.4928793139100267 //second time running the program: // 0.5420802533292215 // third time running the program: // 0.5479835477696466 3 sebagai angka awal dan bukan console.log(Math.random()); // You will not get the same output //first time running the program: // 0.4928793139100267 //second time running the program: // 0.5420802533292215 // third time running the program: // 0.5479835477696466 2
Yang mengatakan, ada cara bagi Anda untuk menentukan rentang angka yang inklusif. Untuk ini, Anda perlu menentukan console.log(Math.ceil(6.2)); // 7 console.log(Math.ceil(6.3)); // 7 console.log(Math.ceil(6.5)); // 7 console.log(Math.ceil(6.8)); // 7 2 dan console.log(Math.ceil(6.2)); // 7 console.log(Math.ceil(6.3)); // 7 console.log(Math.ceil(6.5)); // 7 console.log(Math.ceil(6.8)); // 7 1
Di bagian sebelumnya, Anda melihat cara menghasilkan angka acak antara rentang yang ditentukan, dan kodenya serupa. Satu-satunya perubahan adalah Anda meneruskan hasilnya ke metode console.log(Math.floor(6.2)); // 6 console.log(Math.floor(6.3)); // 6 console.log(Math.floor(6.5)); // 6 console.log(Math.floor(6.8)); // 6 4 untuk membulatkan desimal ke bilangan bulat terdekat
Jadi, kode umumnya adalah sebagai berikut
console.log(Math.round(6.2)); // 6 console.log(Math.round(6.3)); // 6 console.log(Math.round(6.5)); // 7 console.log(Math.round(6.8)); // 7 _4Untuk membuat rentang itu inklusif, Anda akan melakukan hal berikut
console.log(Math.round(6.2)); // 6 console.log(Math.round(6.3)); // 6 console.log(Math.round(6.5)); // 7 console.log(Math.round(6.8)); // 7 5Jadi, untuk menghasilkan angka acak antara angka console.log(Math.random()); // You will not get the same output //first time running the program: // 0.4928793139100267 //second time running the program: // 0.5420802533292215 // third time running the program: // 0.5479835477696466 2 (inklusif) dan Math.random(); 6 (inklusif), Anda akan menulis yang berikut ini
console.log(Math.round(6.2)); // 6 console.log(Math.round(6.3)); // 6 console.log(Math.round(6.5)); // 7 console.log(Math.round(6.8)); // 7 _6Kesimpulan
Dan begitulah. Anda sekarang tahu cara kerja metode console.log(Math.round(6.2)); // 6 console.log(Math.round(6.3)); // 6 console.log(Math.round(6.5)); // 7 console.log(Math.round(6.8)); // 7 _7 dan beberapa cara untuk menggunakannya
Untuk mempelajari lebih lanjut tentang JavaScript, buka Algoritma JavaScript dan Sertifikasi Struktur Data freeCodeCamp
Ini adalah kurikulum gratis, dipikirkan dengan matang, dan terstruktur di mana Anda akan belajar secara interaktif. Pada akhirnya, Anda juga akan membangun 5 proyek untuk mengklaim sertifikasi dan memperkuat pengetahuan Anda
Terima kasih sudah membaca
IKLAN
IKLAN
IKLAN
IKLAN
IKLAN
IKLAN
IKLAN
IKLAN
Mempelajari sesuatu yang baru setiap hari dan menulis tentangnya
Jika artikel ini bermanfaat, tweetlah
Belajar kode secara gratis. Kurikulum open source freeCodeCamp telah membantu lebih dari 40.000 orang mendapatkan pekerjaan sebagai pengembang. Memulai