How to Select a Random Element from Array in TypeScript ? Last Updated : 28 Apr, 2025 Comments Improve Suggest changes Like Article Like Report We are given an array in TypeScript and we have to select a random element every time the code runs. It will automatically select a new random element every time and return it. The below approaches can be used to accomplish this task: Table of Content Using Math.random() functionUsing splice() methodUsing Math.random() functionThe “Math.random()” function generates a random number every time between 0 and 1.Multiply it with the length of the array to get the numbers between 0 and the length of the array.Use the “Math.floor()” method to get the index ranging from (0 to array.length-1).Example: The below code implements the above-specified methods to select a random element from a TypeScript array. JavaScript let arr: number[] = [1, 2, 3, 4, 5]; if (arr.length === 0) { console.log(undefined); } else { const ind: number = Math.floor(Math.random() * arr.length); const result: number = arr[ind]; console.log(`Random Element = ${result}`); } Output: Random Element = 3Using splice() methodFirst the random index is generated by using the random() method.Now, the splice() method removes one element from the array at the randomly generated index. The splice method returns an array containing the removed elements.The array returned will be of size 1 and the element which it contains is indeed the random element.Example: The below code is practical implementation of splice() method to get the random element from array. JavaScript let arr: number[] = [1, 2, 3, 4, 5, 6, 8, 7]; if (arr.length === 0) { console.log(undefined); } else { const ind = Math.floor(Math.random() * arr.length); const result = arr.splice(ind, 1)[0]; console.log(`Random Element = ${result}`); } Output: Random Element = 2 Comment More infoAdvertise with us Next Article How to Select a Random Element from Array in TypeScript ? P pulkitagrawal777 Follow Improve Article Tags : JavaScript Web Technologies TypeScript Similar Reads How to Select a Random Element from a Tuple in Python Selecting a random element consists of retrieving one element from a collection in an unpredictable manner. In Python, this can be done easily using different methods provided by the random module. Below are the three different approaches to select a random element from a tuple. Select a Random Elem 2 min read How to Declare a Fixed Length Array in TypeScript ? To declare a Fixed-length Array in TypeScript you can use a Tuple. Tuple types allow you to specify the types for each element in the array and, importantly, define a fixed number of elements in a specific order. In this article, we are going to learn how to declare a fixed-length array in TypeScrip 3 min read How to find the Total Number of Elements in an Array in TypeScript ? In TypeScript, arrays are a common data structure used to store collections of elements. You can store multiple elements of different or the same data type inside them by explicitly typing. The below methods can be used to accomplish this task: Table of Content Using the length property Using the fo 3 min read How to Declare an Array of Strings in TypeScript ? Arrays are fundamental data structures in TypeScript, enabling developers to manage collections of elements efficiently. Below are the approaches to declare an Array of strings in TypeScript:Table of ContentSquare Brackets NotationArray ConstructorSquare Brackets NotationUsing square brackets notati 1 min read How to randomly select elements of an array with NumPy in Python ? Randomly selecting elements from an array means choosing random elements from the array. NumPy offers several efficient methods to pick elements either with or without repetition. For example, if you have an array [1, 2, 3, 4, 5] and want to randomly select 3 unique elements, the output might look l 2 min read Like