PHP Classes

PHP Random people: Generate random people names and other data

Recommend this page to a friend!
  Info   Example   Screenshots   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
StarStarStarStar 71%Total: 449 All time: 6,192 This week: 62Up
Version License PHP version Categories
random-people 1.0.2BSD License5.4PHP 5, User Management, Testing
Description 

Author

This class can generate fake random people names and other data.

It can generate random data for people like names, gender and birth dates.

The data is retrieved from configuration script that are currently made available in English and Russian.

A subclass can create random family trees with desired depth in terms of number of generations.

Innovation Award
PHP Programming Innovation award nominee
May 2015
Number 3


Prize: One downloadable copy of PhpED Professional
Many applications need to be tested using common types of data that they need to handle.

One of the most common types of data applications need to handle is data of real people, like their names, gender, birth dates, etc..

This class can generate random data for testing applications that handle people data. It can generate names for both genders in either English or Russian.

Manuel Lemos
Picture of Alexander Selifonov
  Performance   Level  
Name: Alexander Selifonov is available for providing paid consulting. Contact Alexander Selifonov .
Classes: 20 packages by
Country: Russian Federation Russian Federation
Age: 62
All time rank: 502 in Russian Federation Russian Federation
Week rank: 128 Down9 in Russian Federation Russian Federation Down
Innovation award
Innovation award
Nominee: 16x

Winner: 2x

Example

<?php
/**
* @name employees.php
* Using class.randomdata.php example
* (make random employee list with birthdays, start-working dates, dept ID's)
* @Author Alexander Selifonov, <alex [at] selifan {dot} ru>
* To generate randomized russian people, use parameter "lang" :
* example.php?lang=ru
*
**/

include('../src/class.randomdata.php');
$lang = isset($_GET['lang']) ? $_GET['lang'] : 'en';
include_once(
"../src/class.randomdata.lang-$lang.php");

$sex_arr = array('m', 'f');

// add my attribs for employee person: "Start working date" and "Dept name"
#RandomData::registerAttribute('startwork', 'randStartWork');
RandomData::registerAttribute('startwork', function($par) {
   return
RandomData::getRandomDate(1,15);
});

RandomData::registerAttribute('dept', 'randDeptName');
RandomData::setConfig('birthdate', array('min'=>21,'max'=>70));

echo
"Generated employees : <table border='1'><tr><th>No</th><th>Name</th>"
  
. "<th>gender</th><th>birth date</th><th>Start work</th><th>Department</th></tr>";

$options = array('birthdate'=>true /*array(19,20)*/, 'dateformat'=>'Y-m-d','middlename'=>true);

/** if you need multiple language in your list (english and russian in my case), uncomment this line:
* $options['lang'] = array('en','ru');
* In that case lnguage will be randomly selected from that list for each person.
**/

for($kk=1; $kk<=50; $kk++) {

   
$person = RandomData::getPerson($options);
    echo
"<tr><td>$kk<M/td><td> $person[lastname], $person[firstname] $person[middlename] </td>"
      
. "<td>$person[gender]</td><td>$person[birthdate]</td>"
      
. "<td>$person[startwork]</td><td>$person[dept]</td></tr>";
}

echo
"</table>";

exit;

function
decodeSex($sx) {
    return ( (
$sx === 'f') ? 'female' : 'male');
}

# creates random "started working" date for employee
function randStartWork($par=0) {
   
$ret = RandomData::getRandomDate(1,15); // random from 1 to 15 years from current date
   
return $ret;
}

/**
* Random Department creator
*
* @param mixed $par not used yet!
*/
function randDeptName($par=0) {
   
$dept_arr = array( // List all your departments (names or ID's) here !
      
'Head department'
     
,'Accounting department'
     
,'Security department'
     
,'Sales department'
     
,'Marketing dept'
     
,'HR'
     
,'IT'
   
);
   
$deptid = rand (0, count($dept_arr)-1);
    return
$dept_arr[$deptid];
}


Details

Extensible Random People Generator

Class for generating random people data, for populating test databases and similar purposes (for example create company fake employees list, or obfuscating personnel data before sending outside company)

generating sample

Description

Simple module for generating randomized "human" data: First name, Last name, middle name (if needed), birthdate in desired age range.

Functions for generating any additional attributes can be easily added. For example. you can add "department", "duty" or "salary" generators to make full emlpoyee data.

Included "language" modules for english and russian people names.

Using

include_once('src/class.randomdata.php');
include_once('src/class.randomdata.lang-en.php'); // your language module can be here !

$sex_arr = array('m', 'f');

$gender = $sex_arr[rand(0,1)];

$ln = RandomData::getLastName($gender); // get random last name
$fn = RandomData::getFirstName($gender); // get random first name
$pn = RandomData::getMiddleName($gender); // and patronimnic/middle name
$birth = RandomData::getDate(4,50, 'Y-m-d'); // random birth date for 4 - 50 years old, 'YYYY-MM-DD'
echo "$ln, $fn $pn, ". ($gender==='f'?'female':'male') . ", born: $birth<br>";

You can add your language modules just by creating class.randomdata.lang-XX.php file (XX must be your language abbreviation) and including it to your script. Example content of implementation for on "language":

// file: myrandomdata.en.php
$options = array(
  'firstnames' => array(
    'm' => array( // male first names
    'Aaron', 'Abbott', 'Abel', 'Abner', 'Abraham', 'Adam', 'Addison', 'Adler', 'Adley', 'Adrian', 'Aedan'
    // ...
    )
    ,'f' => array( // female first names
    'Abigail', 'Ada', 'Adelaide', 'Adrienne', 'Agatha', 'Agnes', 'Aileen', 'Aimee', 'Alanna', 'Alarice', 'Alda'  )
    // ...
    )
  )
  ,'lastnames' => array('Abbott','Beckham','Black','Braxton','Brennan' /.../ )
  ,lastname_modifier' => NULL
);

RandomData::registerLanguage('xx', $options); // Now "active" language for randomdata is "xx"

Or you can add some 'basic' names to earlier pre-defined lists. For examle, to add some new last-names to the 'english' list:

$my_firstnames = array('John', 'Mike'); // Base names to add
RandomData::addSource('firstnames', 'm', $my_firstnames); // add to male First names

$my_lastnames = array('Johnson', 'Harvester');
RandomData::addSource('lastnames', null, $my_lastnames); // Last names

  • "firstnames" sub-array should contain all possible first names for current language, sub-array 'm' - for male names, 'f' - fo female's.
  • "lastnames" should contain "last names" that will be used in "randomized people" creation process.
  • If patronimic (or middle) names used in your country, populate 'patrnames' sub-array (it must contain two subarrays too, 'm' amd 'f').

If last name for males and females has differense, you can pass "modifier" function name, that will correctly modify base last name by adding respective postfix, "lastname_modifier" element must pass "modifier" user function name that will receives two parameters - "base" last name and gender ('m' or 'f') and returns correct lastname for passed gender. For example, russian last names based on common "base" usually have different: endings:

  • Male: Sidorov, Maltsev, Karpin
  • Female: Sidorova, Maltseva, Karpina

So a simplest modifier function (for russian last names) should at least add "a" at the end of a last name for female (in most cases, with exclusions).

Setting limits for birthdate

By default random birth date range is between 1 and 60 years ago from current date. (see a var $config in the main class module). To change these borders you can call function RandomData::getRandomDate() explicitly passing your min and max year values. But when "integrate" function getPerson() for employee list (for instance), you will want to make "mature" people. So you can pass your limits in $options parameter :

$options = array('birthdate'=>array(20,60));
RandomData::getPerson($options);
// ...

Or you can call setConfig() method before any getPerson() using :

RandomData::setConfig(array(
  'birthdate'=>array(
      'min'=>20
    , 'max'=>60
  )
));

Adding user attributes

It is possible to add new atributes to be randomly generated for the object (person). For example, you may need "department id", "salary" or a start working date for the full employee description.

// Prepare function that returns random date value as a "start working date"
function randStartWork($par=0) {
    $ret = RandomData::getRandomDate(1,15); // random from 1 to 15 years from current date
    return $ret;
}
RandomData::registerAttribute('startwork', 'randStartWork');

// or in "closure" manner (PHP 5.3+):
RandomData::registerAttribute('startwork',
   function() {
     return RandomData::getRandomDate(1,15);
   }
);

Getting randomized person data

First, You can call single function for each person attribute:

$sex_arr = array('m', 'f');
$sex = $sex_arr[rand(0,1)];
$lastname  = RandomData::getLastName($sex);
$firstname = RandomData::getFirstName($sex);
$midname   = RandomData::getMiddleName($sex);
$birthdate = RandomData::getRandomDate(20,50, 'Y-m-d');

Second, you call static method getPerson() that wll return associative array holding randomized values for all attributes - "built-in" and added by you :

$person = RandomData::getPerson();
$db->append('employees', $person); // here must be your operator for adding data to DB

Family tree generation

Additional class was included to generate random "family trees", begining from "start" person, with his/her parents, grand-parents etc. This class implemented as separated module, class.randomdata.ftree.php This module contains class RandomFtree that extends base class RandomData. It has one main public method familyTree(), that will return an array with "family tree" people.

Each row in this array is an array too, that contains one or more "person" blocks. There is one person in the first row - this is a "start" person of a tree. Each next row contains parents (father first, then mother) for all person(s) in the previous row. It is possible to generate death date too. To turn it on. pass $options with non-empty 'death' element:

$opts = array('generations' => 2, 'death'=>80);
$tree = $randtree->familyTree($opts);

Here we want to create two generations (parents and grand-parents) from start person, with "death date", having "maximal" age of 80 years. One note: if year of created death date is equal or greather than current year, person will be treated as "alive" (no death date).

Working sample demonstrating created family tree provided in "examples" folder : examples/family_tree.php

To visualize family tree i used CSS3 tricks from http://codepen.io/Pestov/pen/BLpgm :

Created family tree example

Working examples can be found in examples folder.

License

Distributed under BSD (v3) License : http://opensource.org/licenses/BSD-3-Clause


Screenshots (1)  
  • examples/shot-family-tree.png
  Files folder image Files (10)  
File Role Description
Files folder imageexamples (3 files)
Files folder imagesrc (4 files)
Accessible without login Plain text file README.md Doc. Read me
Accessible without login Image file screenshot.png Data screenshot

  Files folder image Files (10)  /  examples  
File Role Description
  Accessible without login Plain text file employees.php Example Using example (recommended using)
  Accessible without login Plain text file family_tree.php Example Sample script for family tree
  Accessible without login Plain text file simple_people.php Example Using example

  Files folder image Files (10)  /  src  
File Role Description
  Plain text file class.randomdata.ftree.php Class Subclass for creating family trees
  Accessible without login Plain text file class.randomdata.lang-en.php Conf. English names module
  Accessible without login Plain text file class.randomdata.lang-ru.php Conf. Russian names module
  Plain text file class.randomdata.php Class Main class

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:449
This week:0
All time:6,192
This week:62Up
User Ratings User Comments (1)
 All time
Utility:91%StarStarStarStarStar
Consistency:91%StarStarStarStarStar
Documentation:83%StarStarStarStarStar
Examples:83%StarStarStarStarStar
Tests:-
Videos:-
Overall:71%StarStarStarStar
Rank:215
 
Sometimes you see something and ask.
9 years ago (Dave Smith)
80%StarStarStarStarStar