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

๐ŸŒ
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().
๐ŸŒ
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
๐ŸŒ
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('') });
๐ŸŒ
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));
๐ŸŒ
Programiz
programiz.com โ€บ javascript โ€บ examples โ€บ get-random-item
JavaScript Program to Get Random Item From an Array
JavaScript String replaceAll() ... 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 const item = arr[randomIndex]; return item; } const array = [1, 'hello', ...
๐ŸŒ
W3Resource
w3resource.com โ€บ javascript-exercises โ€บ javascript-array-exercise-35.php
JavaScript array: Get a random item from an array - w3resource
July 12, 2025 - // 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 โ€บ 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
๐ŸŒ
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
๐ŸŒ
CodingTechRoom
codingtechroom.com โ€บ question โ€บ generate-random-string-from-array-javascript
How to Generate a Random String from an Array of Strings in JavaScript? - CodingTechRoom
const stringsArray = ['apple', 'banana', 'cherry', 'date']; const randomString = stringsArray[Math.floor(Math.random() * stringsArray.length)]; console.log(randomString);
๐ŸŒ
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)];
๐ŸŒ
Medium
byrayray.medium.com โ€บ how-to-create-a-random-string-with-maximum-characters-in-javascript-389ea3698721
How To Create a Random String With Maximum Characters in JavaScript | by RayRay | Better Programming
August 25, 2022 - This post is a follow-up to my previous post How To Create a Random String with JavaScript. If you havenโ€™t checked my previous post, I highly recommend checking it out. In this, we will continue with that last function from that post. function randomString() { return [...Array(5)].map((value) => (Math.random() * 1000000).toString(36).replace('.', '')).join(''); }
๐ŸŒ
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!
๐ŸŒ
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 ...