var textArray = [
    'song1.ogg',
    'song2.ogg'
];
var randomNumber = Math.floor(Math.random()*textArray.length);

audioElement.setAttribute('src', textArray[randomNumber]);
Answer from Gideon on Stack Overflow
🌐
W3Resource
w3resource.com › javascript-exercises › javascript-array-exercise-35.php
JavaScript array: Get a random item from an array - w3resource
July 12, 2025 - Write a JavaScript function that returns a random element from an array using Math.random() and floor.
Discussions

Is it possible to get a random letter from a string element inside an array?
Say you have the following array: let characters = ['ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz', '1234567890', '!@#$%^&*()']; Would it be possible to get a random string, from any index? Say I wanted at least 1 string value from index 0. Could I loop through the array, and grab ... More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
1
0
October 3, 2019
Random String
Add the strings to an array const words = ['jon','jack','jill'] Math.random() generates a random number between 0-1 in Javascript. Math.floor() rounds a number down to an interger. let randomIndex = Math.floor(Math.random()*words.length + 1) Then access the string from the array, based on the random index. console.log(words[randomIndex]) More on reddit.com
🌐 r/learnjavascript
9
1
December 21, 2021
javascript - Get random string from array list - Stack Overflow
I have the code as below, but can't get it work. It should return 1 value from the myArray, but it returns "undefined". What's the problem? HTML JavaScript var More on stackoverflow.com
🌐 stackoverflow.com
Getting a random value from a JavaScript array - Stack Overflow
Future visitors: Please only add ... to an existing answer, especially if it recommends a poor practice like modifying the array prototype, creating globals, using your brand new third party package, something that runs in O(n), etc. Thanks. ... const months = ["January", "February", "March", "April", "May", "June", "July"]; const random = ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
Is it possible to get a random letter from a string element inside an array? - JavaScript - The freeCodeCamp Forum
October 3, 2019 - Say you have the following array: let characters = ['ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz', '1234567890', '!@#$%^&*()']; Would it be possible to get a random string, from any index? Say I wanted at least 1 string value from index 0. Could I loop through the array, and grab ...
🌐
Programiz
programiz.com › javascript › examples › get-random-item
JavaScript Program to Get Random Item From an Array
To understand this example, you should have the knowledge of the following JavaScript programming topics: ... // program to get a random item from an array function getRandomItem(arr) { // get random index value const randomIndex = Math.floor(Math.random() * arr.length); // get random item ...
🌐
Kirupa
kirupa.com › html5 › picking_random_item_from_array.htm
Picking a Random Item from an Array | kirupa.com
In this article, we will learn how to write this JavaScript ourselves. We will learn how to write some code to randomly pick an item from array filled with items!
🌐
Reddit
reddit.com › r/learnjavascript › random string
r/learnjavascript on Reddit: Random String
December 21, 2021 -

I am new to programming because of my school but how can I make the program choose a random String of the available Strings i have given to him. How can i lead this whole process from beginning to the end I am actually confused because I tried something with array and java.util.Random, but it seems to only do the work with integers in "x"
Thank youu

Find elsewhere
🌐
Sentry
sentry.io › sentry answers › javascript › generate random string/characters in javascript
Generate random string/characters in JavaScript | Sentry
The crypto.getRandomValues method populates the Uint8Array array with random numbers with a value between 0 and 255. We then loop through this array to create a random string.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-select-a-random-element-from-array-in-javascript
JavaScript - Select a Random Element from JS Array - GeeksforGeeks
How to select a random element from an array in JavaScript? The Math.random() method is used to get the random number between 0 to 1 (1 exclusive).
Published   July 12, 2025
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-select-a-random-element-from-array-in-javascript
JavaScript – Select a Random Element from JS Array | GeeksforGeeks
How to select a random element from an array in JavaScript? The Math.random() method is used to get the random number between 0 to 1 (1 exclusive).
Published   January 9, 2025
🌐
Programiz
programiz.com › javascript › examples › generate-random-strings
JavaScript Program to Generate Random String
To understand this example, you should have the knowledge of the following JavaScript programming topics: ... // program to generate random strings // declare all characters const characters ='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; function generateString(length) { let result = ' '; const charactersLength = characters.length; for ( let i = 0; i < length; i++ ) { result += characters.charAt(Math.floor(Math.random() * charactersLength)); } return result; } console.log(generateString(5));
🌐
CSS-Tricks
css-tricks.com › snippets › javascript › select-random-item-array
Select Random Item from an Array | CSS-Tricks
December 23, 2016 - Create a DigitalOcean account and get $200 in free credit for cloud-based hosting and services. ... JS Result EDIT ON function randomNum(minVal, maxVal) { do { r = Math.random(); } while (r == 1); return minVal+Math.floor(r*(maxVal+1-minVal)); } var coolwords = new Array(); coolwords[0] = “robot”; coolwords[1] = “inferno”; coolwords[2] = “giga”; coolwords[3] = “infinity”; coolwords[4] = “pow”; coolwords[5] = “smash”; coolwords[6] = “boom”; coolwords[7] = “crunch”; coolwords[8] = “robot”; coolwords[9] = “inferno”;
🌐
GitHub
gist.github.com › yairEO › f75de80b147c81fd688ffd323f8ad6a2
Generate javascript array with random strings · GitHub
To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters ... Array.apply(null, Array(10)).map(function() { return Array.apply(null, Array(~~(Math.random() * 10 + 3))).map(function() { return String.fromCharCode(Math.random() * (123 - 97) + 97); }).join('') });
🌐
Vultr Docs
docs.vultr.com › javascript › examples › get-random-item-from-an-array
JavaScript Program to Get Random Item From an Array | Vultr Docs
December 18, 2024 - The succinct ES6 version of getRandomItem easily handles arrays containing various data types, fetching a random element each time. Utilizing JavaScript to select a random item from an array is a useful skill that can be applied in numerous programming scenarios, from game development to data ...
🌐
CodingTechRoom
codingtechroom.com › question › generate-random-string-from-array-javascript
How to Generate a Random String from an Array of Strings in JavaScript? - CodingTechRoom
Generating a random string from an array is a common task in programming. In JavaScript, this involves using the built-in Math.random() function to select an index from the array randomly. ... function getRandomString(stringsArray) { const randomIndex = Math.floor(Math.random() * ...
🌐
IQCode
iqcode.com › code › javascript › random-string-from-array-javascript
random string from array javascript Code Example
October 17, 2021 - var myArray = [ "Apples", "Bananas", "Pears" ]; var randomItem = myArray[Math.floor(Math.random()*myArray.length)]; ... var textArray = [ 'song1.ogg', 'song2.ogg' ]; var randomNumber = Math.floor(Math.random()*textArray.length); ... var groceries = [ 'milk', 'coriander', 'cucumber', 'eggplant' ...
🌐
QuickRef.ME
quickref.me › home › how to get random items of an array in javascript - quickref.me
How to get random items of an array in JavaScript - QuickRef.ME
In this Article we will go through how to get random items of an array only using single line of code in JavaScript. This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => Arrow Function.
🌐
Delft Stack
delftstack.com › home › howto › javascript › javascript random string
How to Generate Random String in JavaScript | Delft Stack
March 11, 2025 - We then loop through the array, using the modulus operator to select characters from our predefined character set. This approach is ideal for generating secure tokens or passwords, as it provides a level of randomness that is significantly stronger than the previous methods. Using cryptographic functions is essential when security is a priority. In this article, we explored various methods to generate random strings in JavaScript, including using Math.random(), custom character sets, and the more secure Crypto.getRandomValues().