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
Number(out) : out; } case "string": { wasString = true; input = input.split(""); } case "object": { if (!Array.isArray(input)) { throw new Error("objects are not supported here"); } if (typeof count != "number" && typeof count != "bigint") { throw new Error("you need to specify a count"); } const contentCount = BigInt(input.length); const bitCount = countBits(contentCount); const out = [...Array(count)].map(_=> { return input[getN(contentCount, bitCount)]; }); return wasString ? out.join("") : out; } } }; })(); random("such String", 20); would return a string with 20 characters randomly made out of the given input string.
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.
๐ŸŒ
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 ...
๐ŸŒ
Zipy
zipy.ai โ€บ blog โ€บ generate-random-string-characters-in-javascript
generate random string characters in javascript
April 12, 2024 - Happy coding, and may your strings always be as random as needed! Basic Javascript concepts for everyday development ยท 20 everyday Javascript errors you should know: A guide on how to fix Javascript errors ยท Master JavaScript Debugging: Strategies and Best Practices ... Deciphering Truthy and Falsy Values in JavaScript: A Developer's Guide to Writing More Effective Code ยท JavaScript Mastery: Appending to Arrays Simplified