Archive

Posts Tagged ‘script’

Random name generator in Python

September 4th, 2010 Niels No comments

In this short tutorial I will show you how to make a random name generator in Python.

If you came here looking for some cool random names visit the online name generators.

The name generator script

Open IDLE which is the Python supplied IDE or editor (Go to the bottom of the post to see, how you get started with Python).

Open a new window from the file menu and write the following name generator script into this window.

def name_generator():
  import random
  color = ["Red","Blue","Black","White"]
  tool = ["Hammer","Drill","Cutter","Knife"]
  randomNumber1 = random.randrange(0,len(color))
  randomNumber2 = random.randrange(0,len(tool))
  name = color[randomNumber1] + " " + tool[randomNumber2]
  print(name)

name_generator()

(Be aware that copy-paste from the blog may give syntax errors!
 A correct indent is an important element Python syntax!)

Save it as name-generator.py.  Now you can run the python program by pressing F5.

Name generator walk through

Lets walk through the python script, line by line.

  1. In the first line we define our name generator function.
  2. Then we import the random module, so we can call the random function later on.
  3. In line define 2 arrays (color and tool) and asign values to them
  4. Then we make 2 random numbers based on the length of our arrays. The randrage() function returns an integer in the range defined by the parameters. As parameters we use 0 and the len() function to count the numbers of words in the arrays.
  5. In line 7 and 8 we make the final name and print it on the screen.
  6. Finally we call the name generator function, we have just written.

That’s all it takes to make a simple Python random name generator. When you this basic functionality it’s easy to experiment and develop the random name generator to suit your needs and wishes.

Name generator inspiration

For inspiration visit the name generators where you can find a lot of name generators in several genres. From random names over fantasy and nicknames to band and business names.

Python Beginner guides and tutorials

Python is a free scripting language running on all platforms from Linux to Windows. Python is a relative easy programming language and a name generator is a simple application so it’s a good place to start if you wanna learn some programming.

If you haven’t Python installed at your computer you can download it at Python.org. This name generator script is based on python 3.12, which is the newest stable version at the moment.

At python.org you can find an excellent Python tutorial. And you can find much more about how to use the Random function in Python.

Or visit this beginner guide to Python which contains lots of links and resources.

Choose random person

July 25th, 2010 Niels No comments

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

Name generator script

May 11th, 2010 Niels 1 comment

I have made a  complete name generator script to download and use as your own. The script contains a full functional HTML page with all necessary JavaScript code, HTML and CSS, and can be run in any browser.

The name generator script is a complete do-it-yourself name generator! Ready to run with or without changes. The script is a simplified version one of the one I use at all my name generators.

The script is thoroughly explained and commented in the download file. So it is very easy  to work with for everybody, even for beginners to web programming. If you like further explanation on how the script works visit my How to make a name generator tutorial, where i you through a only slightly more simple generator script.

To get up and running just download the script, change the text and change the word arrays to your likings.  And you got yourself your own personal name generator.

How to use the name generator script

Getting started

Download the name-generator-script.zip.

Or just visit the name generator script demo, right click the page. Select view source. Copy the source to a text-editor.
Save is at generator.html.

How to run it

It’s easy. Just open the html file with your favorite browser.

How to edit it

To edit this page open it with a text editor with syntax highlighting. I’ll strongly recommend notepadd++ a free, fast and flexible text editor, I also use myself.

Name Generator FAQ

No one yet. Guess the code must be  self-explanatory. ;)   Well seriously… if you need help just ask your questions below. I’ll try my best to help you.

How to get visitors on your new name generator

If you make a cool name generator, I might wanna make a review of it. Just write a comment and I’ll look into it.

How to make a name generator

May 8th, 2010 Niels 2 comments

In this tutorial I’ll show how to make a simple name generator. A sort of  “build your own name generator” to experiment with. You just need to add your own word lists and you’re up and running.  But you can expand and modify the  name generator code to your own likings and abilities.

The name generator tutorial is very simple and are aimed at absolute beginners in web programming.

But enough talk, here is some name generator code that makes superhero names:

function generator(){
  // Add your own words to the wordlist. Be carefull to obey the showed syntax
  var adjectives = ["Cool","Masked","Bloody","Lame"]
  var animals = ["Hamster","Moose","Lama","Duck"]

  // Random numbers are made
  var randomNumber1 = parseInt(Math.random() * adjectives.length);
  var randomNumber2 = parseInt(Math.random() * animals.length);
  var name = adjectives[randomNumber1] + " " + animals[randomNumber2];

  alert(name); //Delete this when the below works            

  //If there's already a name it is removed  
  if(document.getElementById("result")){
    document.getElementById("placeholder").removeChild(document.getElementById("result"));
  }
  // A div element is created to show the generated name.
  //The Name is added as a textnode. Textnode is added to the placeholder.
  var element = document.createElement("div");
  element.setAttribute("id", "result");
  element.appendChild(document.createTextNode(name));
  document.getElementById("placeholder").appendChild(element);
}

The script is a simplified version of the one I use to make superhero names with my superhero generator.

The name generator piece by piece

The arrays

The most important part of  the name generator is the word lists. The more words in the list the more variety in the output. So the first part to do is to create a couple of arrays holding all our cool words:

var adjectives = ["cool","masked","bloody","lame"];
var animals = ["hamster","moose","lama","duck"]

To retrieve data  from the list we call the name of array with the count of the word as a parameter in a square bracket.  Be aware that counting in programming almost always starts at zero and not at 1.

So adjectives[0] is cool and  animals[1] gives moose

Randomness

We of course want random names, so we need to replace the hard coded numbers with random numbers. In our case numbers between 0 and 3 because we have 4 words in our lists. Instead of counting the words ourself, we do that we let the JavaScript length function do the counting. Then we multiply it with a random floating-point number between 0 and 0.9999999999999. Finally we use the parseInt funktion to make the floating-point number into a integer.

All sounds a bit complicated but code wise  it’s  quite easy.

randomNumber1 = parseInt(Math.random() * adjectives.length);
randomNumber2 = parseInt(Math.random() * animals.length);

The output

The most simple way to output the name is using the alert function. I use it only to check the generated name cause,  the alert box is  very annoying. So just delete the line, when the code below is up an running.

We will use 7 extra lines to integrate the result into the web page in a more natural way.

But before we begin you need to make an empty div tag in your HTML:

<div id=”placeholder”></div>

And here are the 7 lines that write the result in the page..

if(document.getElementById("result")){
  document.getElementById("placeholder").removeChild(document.getElementById("result"));
}

element = document.createElement("div");
element.setAttribute("id", "result");
element.appendChild(document.createTextNode(name));
document.getElementById("placeholder").appendChild(element);

In short it works like this:

  1. If there’s already a result it is removed
  2. Then a new div element to hold the result  is created
  3. The name is added as a text node
  4. Finally the element is appended to a placeholder in the HTML.

When this part works, you can delete the alert(name) from the script.

Putting it all together

The generator function can either be included in the head of the HTML document or you can place it in a external JavaScript file.

Now you can call the function from your HTML page.  I always do it 2 places. First in the body tag with an onload event. The generator function then also runs when the page is loaded.

<body onload=”generator()”>

And then I also call the JavaScript function on a button:

<input type=”button” onclick=”generator()” />

Download the name generator script

Download name generator script here and read the description of how to make the name generator script work.

Want a review of your name generator?

Write a comment if  you make a cool name generator with this script. I might very well  write a review of it.  :)

Also remember to visit my name generators to see  how I make superhero names with the superhero generator.