Hướng dẫn random array javascript

Last update on August 19 2022 21:50:49 [UTC/GMT +8 hours]

JavaScript Array: Exercise-35 with Solution

Write a JavaScript function to get a random item from an array.

Pictorial Presentation:

Sample Solution:

HTML Code:




  
  JavaScript function to get a random item from an array.






JavaScript Code:

function random_item[items]
{
  
return items[Math.floor[Math.random[]*items.length]];
     
}

var items = [254, 45, 212, 365, 2543];
console.log[random_item[items]];

Sample Output:

365

Flowchart:

ES6 Version:

function random_item[items]
{
  
return items[Math.floor[Math.random[]*items.length]];
     
}

const items = [254, 45, 212, 365, 2543];
console.log[random_item[items]];

Live Demo:

See the Pen JavaScript - Get a random item from an array - array-ex- 35 by w3resource [@w3resource] on CodePen.

Improve this sample solution and post your code through Disqus

Previous: Write a JavaScript function to get nth largest element from an unsorted array.
Next: Write a JavaScript function to create a specified number of elements and pre-filled numeric value array.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

JavaScript: Tips of the Day

Operators precedence

console.log[3 + 4 + '5'];

Operator associativity is the order in which the compiler evaluates the expressions, either left-to-right or right-to-left. This only happens if all operators have the same precedence. We only have one type of operator: +. For addition, the associativity is left-to-right.
3 + 4 gets evaluated first. This results in the number 7.
7 + '5' results in "75" because of coercion. JavaScript converts the number 7 into a string, see question 15. We can concatenate two strings using the +operator. "7" + "5" results in "75".

Ref: //bit.ly/323Y0P6

  • Exercises: Weekly Top 12 Most Popular Topics
  • Pandas DataFrame: Exercises, Practice, Solution
  • Conversion Tools
  • JavaScript: HTML Form Validation
  • SQL Exercises, Practice, Solution - SUBQUERIES
  • C Programming Exercises, Practice, Solution : For Loop
  • Python Exercises, Practice, Solution
  • Python Data Type: List - Exercises, Practice, Solution
  • C++ Basic: Exercises, Practice, Solution
  • SQL Exercises, Practice, Solution - exercises on Employee Database
  • SQL Exercises, Practice, Solution - exercises on Movie Database
  • SQL Exercises, Practice, Solution - exercises on Soccer Database
  • C Programming Exercises, Practice, Solution : Recursion

- Ở bài học trước, tôi đã giới thiệu sơ qua về phương thức random[] của đối tượng Math, nó dùng để tạo một số ngẫu nhiên trong khoảng từ 0 đến 1 [không bao gồm số 1]

- Tuy nhiên không dừng lại ở đó, nếu phương thức random[] được sử dụng kết hợp với các phương thức khác thì nó có thể tạo ra những số ngẫu nhiên đa dạng hơn.

- Ở bài hướng dẫn này, tôi sẽ giới thiệu đến bạn kỹ thuật để tạo một số ngẫu nhiên đa dạng hơn thông qua những ví dụ.

1] Tạo một số nguyên ngẫu nhiên

Tạo một số nguyễn ngẫu nhiên trong đoạn từ 0 đến 9


    Math.floor[Math.random[] * 10];

Xem ví dụ

Tạo một số nguyễn ngẫu nhiên trong đoạn từ 0 đến 10


    Math.floor[Math.random[] * 11];

Xem ví dụ

Tạo một số nguyễn ngẫu nhiên trong đoạn từ 0 đến 99


    Math.floor[Math.random[] * 100];

Xem ví dụ

Tạo một số nguyễn ngẫu nhiên trong đoạn từ 0 đến 100


    Math.floor[Math.random[] * 101];

Xem ví dụ

Tạo một số nguyễn ngẫu nhiên trong đoạn từ 1 đến 10


    Math.floor[Math.random[] * 10] + 1;

Xem ví dụ

Tạo một số nguyễn ngẫu nhiên trong đoạn từ 1 đến 100


    Math.floor[Math.random[] * 100] + 1;

Xem ví dụ

Tạo một số nguyễn ngẫu nhiên trong đoạn từ 3 đến 7


    Math.floor[Math.random[] * 5] + 3;

Xem ví dụ

Tạo một số nguyễn ngẫu nhiên trong đoạn từ 3 đến 9


    Math.floor[Math.random[] * 7] + 3;

Xem ví dụ

2] Xây dựng hàm dùng để tạo số nguyên ngẫu nhiên

- Ta thấy trong mỗi ví dụ phía trên, giá trị trả về luôn nằm trong một đoạn nhất định, ví dụ:

  • Từ 0 đến 9
  • Từ 1 đến 100
  • Từ 3 đến 7
  • ....

    ==> Điều đó thật hạn chế nếu như ta muốn tạo nhiều số nguyên trong nhiều đoạn khác nhau.

- Từ đây, để giải quyết vấn đề này thì chúng ta nên xây dựng một hàm tạo số nguyên ngẫu nhiên, chỉ với việc thay đổi giá trị của tham số là ta đã có thể xác định được một đoạn mới.

Tạo một số nguyên ngẫu nhiên trong đoạn từ "min" đến "max" [không bao gồm max]


    function TaoSoNgauNhien[min, max]{
        return Math.floor[Math.random[] * [max - min]] + min;
    }
    var a = TaoSoNgauNhien[1, 10]; //Trả về một số ngẫu nhiên từ 1 đến 9
    var b = TaoSoNgauNhien[3, 8]; //Trả về một số ngẫu nhiên từ 3 đến 7
    var c = TaoSoNgauNhien[2, 5]; //Trả về một số ngẫu nhiên từ 2 đến 4

Xem ví dụ

Tạo một số nguyên ngẫu nhiên trong đoạn từ "min" đến "max" [bao gồm max]


    function TaoSoNgauNhien[min, max]{
        return Math.floor[Math.random[] * [max - min + 1]] + min;
    }
    var a = TaoSoNgauNhien[1, 10]; //Trả về một số ngẫu nhiên từ 1 đến 10
    var b = TaoSoNgauNhien[3, 8]; //Trả về một số ngẫu nhiên từ 3 đến 8
    var c = TaoSoNgauNhien[2, 5]; //Trả về một số ngẫu nhiên từ 2 đến 5

Xem ví dụ

Chủ Đề