Guide to random names – more complex randomness

In this guide I’ll show you how to make complex random algorithms to use with my name generator script to make better and more variated random names.

The example in this guide is taken from my Random Name Generator, which makes random names based on American name lists with the 1000 most popular boy names, girl names and surnames.

Random names script

Here’s the part of the script that handles the randomness:

var n1m = parseInt(Math.random() * boyName.length);
var n1mex = parseInt(Math.random() * boyName.length);
var n1f = parseInt(Math.random() * girlName.length);
var n1fex = parseInt(Math.random() * girlName.length);
var n2 = parseInt(Math.random() * surname.length);
var n2ex = parseInt(Math.random() * surname.length);

var boyName = boyname[n1m];
var boyNameEx = boyName[n1mex];
var girlname = girlName[n1f];
var girlNameEx = girlName[n1fex];
var surname = surname[n2];
var surnameEx = surname[n2ex];

var n3 = parseInt(Math.random()* 100);

if( n3 <= 35){
  name = boyName + " "  + surname;
}else if(n3 > 35 && n3 <= 45 ){
  name = boyName +  " "  + boyNameEx + " " + surname;
}else if(n3 > 45 && n3 <= 48 ){
  name = boyName +  " "  + girlNameEx + " " + surname;
}else if(n3 > 48 && n3 <= 50 ){
  name = boyName +  " "  + surnameEx + " " + surname;
}else if(n3> 50 && n3 <= 85 ){
  name = girlName + " "  + surname;
}else if(n3 > 85 && n3 <= 95 ){
  name = girlName +  " "  + girlNameEx + " " + surname;
}else if(n3 > 95 && n3 <= 98 ){
  name = girlName +  " "  + boyNameEx + " " + surname;
}else{
  name = girlNameEx +  " "  + surnameEx + " " + surname;
}

In line 1-6 we make 6 random numbers. We make a random number for male, female and surnames. And then we make an extra one, for each category. We do that to be able to random names with random middle names.

In line 8-13 we find the random random names and the extra names for the middle names.

In line 15 things starts to get interesting. Here we make the random number, which is the heart in the complex random algorithm. I multiply this number with 100 to make it easier to work with. It makes it easy to convert our randomness numbers into percents.

From line 17 I start to build the random name, and it’s actually pretty self explanatory, what going on. If the number n3 is less than 35 the name will be made from a boy name plus a surname. If the n3 is between 35 and 45 we add the extra boy name as a middle name and so on.

So the above algorithm gives these random names:

  • 35% boy name + surname
  • 10% boy name + boy name as middlename + surname
  • 3% boy name + girl name as middlename + surname
  • 2% boy name + surname as middlename + surname
  • 35% girl name + surname
  • 10% girl name + girl name as middlename + surname
  • 3% girl name + boy name as middlename + surname
  • 2% girl name + surname as middlename + surname

All in all there’s 50% male names and 50% female names.

And it the process we generate a variation in the female and male names. Obviously much more complex than in my basic name generator tutorials, where we just use one random name and combine it with another name.

Even more complexity

But we can make it even more complex, by adding more checks to the above algorithmic randomness rule.

Let’s say we don’t want too long name combinations. We could something like this:

...else if(n3 > 85 && n3 <= 95 ){
  if(girlName.length + surname.length > 13){
    name = girlName +  " "  + girlNameEx + " " + surname;
  }else{
    name = girlName +  " " + surname;
  }
}...

With this example we only ad a middle name to the girl name if the length of the first and the second is less than 13 characters long.

Only your imagination sets the limit for the complexity of your algorithmic randomness rules.

Name generator tutorials

You can find the corresponding name tutorials here:

How to make a name generator. In this name generator tutorial, you’ll find everything you need to know to make a basic name generator.

Name generator script. In the name generator script I have made all the basic work for you. You just need to a little add water to personalize the name generator.

This entry was posted in Name generator stuff and tagged , . Bookmark the permalink.

5 Responses to Guide to random names – more complex randomness

  1. Pingback: How to make a name generator | Name Generators Blog

  2. Brian Wendel says:

    Hello,
    I’ve been playing around with your basic name generator and I was wondering if you know how to add a sound to it when you click the button? I was thinking of either a click or funny short sound. I’ve tried a few things and it always crashes and doesnt work.
    Thank you for putting out such a great website. Its simplicity and clarity are something I could learn alot from.

    Brian

  3. Karen says:

    Hi,

    I was wondering if it was also possible to make the text of the generator result button change every time you click it? Or too complicated? Thanks!

    • Niels says:

      Hi Karen

      You can change the text of any button like this:

      <html>
       <head>
        <script>
      	function changeButtonName(){
      		var myButtonNames =["push me","kick me","press me"];
      		myRandomNumber = Math.floor(Math.random() * myButtonNames.length);
      		document.getElementById("myButton").value = myButtonNames[myRandomNumber];
      	}
        </script>
       </head>
       <body>
        <input type="submit" id="myButton" value="click me" onclick="changeButtonName()"/>
       </body>
      </html>
      

      I pretty much use the same JavaScript functions as in the name generator.

      1. I make an array with the text/strings I want on the button.
      2. Find a random number based on the length of the array.
      3. Add it to thew DOM using the .value syntaks for the input element.

      Good luck.
      Niels

  4. Susan says:

    Hi,

    I was wondering if it was possible to make a generator where you could enter the first letter and it would generate random words beginning with that letter..

    So for every letter I enter it will generate words beginning with that letter?
    It would be great if you could get back to me soon!
    Thanks,

    Susan

Leave a Reply

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