Skip to content
the poor coder

how to choose a random name from a list in javascript

Poor Man
Published
Reading time
2 min read
Text

        //To choose a random name from a list in JavaScript, you can follow the below steps:
        
        //Step 1: Create an array with the list of names.
        var namesList = ["John", "Mary", "David", "Linda", "Sarah", "James", "Jennifer"];
        
        //Step 2: Use the Math.floor() and Math.random() functions to generate a random index from the array
        var randomIndex = Math.floor(Math.random() * namesList.length);
        
        //Step 3: Use the random index to access a random name from the list
        var randomName = namesList[randomIndex];
        
        //Step 4: Print the random name on the console or any other place as per your requirement.
        console.log("Random Name: "+randomName);
    

Above code will work for very small arrays with less than 1000 elements, but if you are working with larger arrays then consider using a more optimized and efficient algorithm. One such algorithm is Fisher-Yates shuffle. It's important to note that this algorithm modifies the original array. Here's how you can implement Fisher-Yates shuffle in JavaScript:

Text

        // Step 1: Create an array with the list of names.
        var namesList = ["John", "Mary", "David", "Linda", "Sarah", "James", "Jennifer"];

        // Step 2: Implement the Fisher-Yates shuffle algorithm
        function shuffle(array) {
            var currentIndex = array.length;
            var temporaryValue, randomIndex;

            while (currentIndex !== 0) {
                randomIndex = Math.floor(Math.random() * currentIndex);
                currentIndex -= 1;
                temporaryValue = array[currentIndex];
                array[currentIndex] = array[randomIndex];
                array[randomIndex] = temporaryValue;
            }

            return array;
        }

        // Step 3: Call the shuffle function on your namesList
        var shuffledList = shuffle(namesList);

        // Step 4: Access a random name from the shuffled list
        var randomName = shuffledList[0];

        // Step 5: Print the random name on the console or any other place as per your requirement.
        console.log("Random Name: "+randomName);
    

Hope this helps!

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC