PHP Classes

PHP Random people: Generate random people names and other data

Recommend this page to a friend!
  Info   View files Example   Screenshots Screenshots   View files View files (9)   DownloadInstall with Composer Download .zip   Reputation   Support forum (2)   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2023-05-22 (1 month ago) RSS 2.0 feedStarStarStarStar 71%Total: 439 All time: 6,243 This week: 103Up
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 <contact>
Classes: 20 packages by
Country: Russian Federation Russian Federation
Innovation award
Innovation award
Nominee: 16x

Winner: 2x

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  
  • examples/shot-family-tree.png
  Files folder image Files  
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  /  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  /  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

 Version Control Unique User Downloads Download Rankings  
 100%
Total:439
This week:0
All time:6,243
This week:103Up
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:313
 
Sometimes you see something and ask.
8 years ago (Dave Smith)
80%StarStarStarStarStar