Choose random person

I made a little script that choose a random person from a list.

The background is that I got this mail from a reader, who need to  choose a random student from his class to answer questions.

Tom wrote:
I am a teacher and we have to randomly choose students to call and answer questions.  Right now we use popsicle sticks with name on stick.  I would like to be able to set up 4 classes,  type in the names of the students from the class and have the app pick a name out of that class randomly.

Beside choosing persons randomly such a random app  also can be used to random pick all kinds of objects or items from a list. Script is programmed in JavaScript.

Choose random person script

Here’s a very simple but useful script, which choose a random student  from a list of names.

function choose_student(){
    // Add your own student names to the class list
    var class_list =  ["Marge","Maggie","Lisa","Bart","Homer","Apu","Burns"];

    // Random numbers are made
    var randomNumber = parseInt(Math.random() * class_list.length);

    // Student are chosen from the class array with the random number
    var name = class_list[randomNumber];

    // Name is shown in alert box
    alert(name);
}
  1. First you make an array (a list ) with the names of the persons you want to choose from
  2. The Math.random() makes random numbers between 0 and 1. By multiplying it with the numbers of students of the list (found with the length function) and flooring it all with the parseInt() function, we will get a number between 0 and numbers of students in the class minus 1.
  3. The person in the position of the random number is extracted from the list and saved in a variable. (the item in the first position in an array is called with the index number 0 like this: myArray[0])
  4. Finally the chosen students name is shown in JavaScript alert box.

And finally you just need to call the “choose random student”-function from your HTML.

<input type="button" value="Chose Random Student" onclick="choose_student()" />

Download example script

You can download an example choose random person script here. In the script you can find both the above pick random person script and the pick random student script I made for Tom. All is integrated in a complete HTML file ready to launch in your favorite browser.

The example script is integrated roughly into my name generator script, but it should give a good overview over how to accomplish the task of picking random persons from simple lists, and how to integrate a simple random application into a HTML page.

Is it the above method truly random?

In short no, but semi randomness is sufficient and adequate for the purpose of selecting random persons for none scientific reasons.

Read more about true random numbers and names.

clipped from web.archive.org
http://web.archive.org/web/20011027002011/http://dilbert.com/comics/dilbert/archive/images/dilbert2001182781025.gif
blog it
This entry was posted in Other generators and tagged , , , . Bookmark the permalink.

3 Responses to Choose random person

  1. Pingback: Call of duty – Weapon and team name generators | Name Generators Blog

  2. Chad says:

    I love the random name generator for students, is there a way to have it randomly pull two names at the same time or more for grouping?

  3. Niels says:

    Hi Chad.

    If you wanna choose random students to form a single group or team, you can add this to the choose random person script:

    var randomNumber2 = parseInt(Math.random() * class_list.length);
    while(randomNumber2 == randomNumber){
        randomNumber2 = parseInt(Math.random() * class_list.length);
    }

    We make a new random number. As long as the number is the same as our first number, we make a new rand0m number.

    If you need another member in the group we simply repeat the step:

    var randomNumber3 = parseInt(Math.random() * class_list.length);
    while(randomNumber3 == randomNumber || randomNumber3 == randomNumber2){
        randomNumber3 = parseInt(Math.random() * class_list.length);
    }
    

    Only difference is that we this check that the new number is not the same as the first one or (or is written like this: || in JavaScript) the same as the second one. For more group or team members just repeat this step. Remember to add the extra or condition to check the 4’th random number also is difference form the 3’rd one.

    The we need to make the random group members:

    var name2 = class_list[randomNumber2];
    var name3 = class_list[randomNumber3];

    Finally we will alert the result:

    alert(name + "n" + name2 + "n" + name3 );

    “n” shows next name on a new line. Good luck. 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *