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
๐ŸŒ
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

๐ŸŒ
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โ€ฆ
๐ŸŒ
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.
๐ŸŒ
Programiz
programiz.com โ€บ javascript โ€บ examples โ€บ get-random-item
JavaScript Program to Get Random Item From an Array
In the above program, a random item from an array is accessed. A random number between 0 to array.length is generated using the Math.random() method. The Math.floor() returns the nearest integer value generated by Math.random(). This random index is then used to access a random array element.
๐ŸŒ
GitHub
gist.github.com โ€บ yairEO โ€บ f75de80b147c81fd688ffd323f8ad6a2
Generate javascript array with random strings ยท GitHub
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('') });
๐ŸŒ
GitHub
gist.github.com โ€บ 6174 โ€บ 6062387
Generate a random string in JavaScript In a short and fast way! ยท GitHub
const rnd = (() => { const gen = (min, max) => max++ && [...Array(max-min)].map((s, i) => String.fromCharCode(min+i)); const sets = { num: gen(48,57), alphaLower: gen(97,122), alphaUpper: gen(65,90), special: [...`~!@#$%^&*()_+-=[]\{}|;:'",./<>?`] }; function* iter(len, set) { if (set.length < 1) set = Object.values(sets).flat(); for (let i = 0; i < len; i++) yield set[Math.random() * set.length|0] } return Object.assign(((len, ...set) => [...iter(len, set.flat())].join('')), sets); })(); console.log('OUTPUT: ', rnd(20)); // Use all sets // OUTPUT: Kr8K1,f5hQa;YJC~7K9z console.log('OUTPUT: ', rnd(20, rnd.alphaLower)); // OUTPUT: cpjbkwvslxsofzvkekcw console.log('OUTPUT: ', rnd(20, rnd.alphaUpper rnd.special)); // OUTPUT: S]|-X]=N}TZC,GE;=,.D
Find elsewhere
๐ŸŒ
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().
๐ŸŒ
W3Resource
w3resource.com โ€บ javascript-exercises โ€บ javascript-array-exercise-35.php
JavaScript array: Get a random item from an array - w3resource
// Arrow function to return a random item from an array const random_item = items => items[Math.floor(Math.random() * items.length)]; // Declare and initialize an array of items const items = [254, 45, 212, 365, 2543]; // Output the result of ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ how-to-select-a-random-element-from-array-in-javascript
JavaScript - Select a Random Element from JS Array - GeeksforGeeks
Selecting a random element from array in JavaScript is done by accessing an element using random index from the array.
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
Selecting a random element from array in JavaScript is done by accessing an element using random index from the array.
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 - var myArray = [ "Apples", "Bananas", "Pears" ]; var randomItem = myArray[Math.floor(Math.random()*myArray.length)];
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ forming-and-matching-strings-of-an-array-based-on-a-random-string-in-javascript
Forming and matching strings of an array based on a random string in JavaScript
We are required to write a JavaScript function that takes in such an array and string as the two argument. Then the function, for each element of the array should check whether that particular element can be formed completely from the string supplied as second argument.
๐ŸŒ
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 ...
๐ŸŒ
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!
๐ŸŒ
Sling Academy
slingacademy.com โ€บ article โ€บ ways-to-generate-random-strings-in-javascript
4 Ways to Generate Random Strings in JavaScript - Sling Academy
const generateRandomString = (length) => { const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; const charactersLength = characters.length; let result = ''; // Create an array of 32-bit unsigned integers const randomValues = new Uint32Array(length); // Generate ...