PHP Classes
elePHPant
Icontem

Chaos Machine One: Generate random data that obeys to formulas

Recommend this page to a friend!
  Info   View files Example   View files View files (43)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2018-12-28 (4 months ago) RSS 2.0 feedNot yet rated by the usersTotal: 57 This week: 1All time: 9,461 This week: 382Up
Version License PHP version Categories
chaosmachineone 1.0GNU Lesser Genera...5Algorithms, PHP 5, Statistics, Math
Description Author

This package can generate random data that obeys to formulas.

It provides several functions that generate data according to well know mathematical functions but the returned values are affected by random error displacements.

Currently it provides functions that generate data based on functions like ramp, logarithm, exponencial, sinus, arc-tangent, parabola, bell, random numbers, text arrays, etc..

Innovation Award
PHP Programming Innovation award nominee
December 2018
Number 6
Many applications take as input data that obeys to well known math formulas.

However, real world data is often affected by errors caused by interference of external systems.

This class can generate data that obeys to given formulas but it also adds "noise" to simulate real world data that can be passed to an application so it can be tested with data that is more realistic.

Manuel Lemos
  Performance   Level  
Name: Jorge Castro <contact>
Classes: 11 packages by
Country: Chile Chile
Age: 43
All time rank: 21668 in Chile Chile
Week rank: 43 Up1 in Chile Chile Up
Innovation award
Innovation award
Nominee: 7x

Winner: 1x

Details

ChaosMachineOne for PHP

A controlled random generator data for PHP. It doesn't chart visually, the objective is to generate values to store into the database (mysql)

Build Status Packagist Total Downloads [Maintenance]() [composer]() [php]() [php]() [CocoaPods]()

What is the objective?

Sometimes we want to generate fake values for the database that are controlled and consistent. So, this library tries to create an ordered chaos.

Let's say the next exercise. We want to generate random values for a new system (sales)

If we generate random values, the chart would look like

> ->gen('when _index<200 then idtable.value=random(-10,10,0.2)')

random

Why is it so random? it is because they are random values (doh). So, they are right but they don't looks real because there is not a trend or a natural flow of information, it is just static noise.

Then, let's generate the same value with a sine (for example, let's say that there is a cycle of sales)

> ->gen('when _index<200 then idtable.add=sin(0,0,10,30)')

random

The chart has a trend but it is too predictable. So, let's add all factors.

> ->gen('when _index<200 then idtable.value=random(-10,10,0.2) and idtable.add=sin(0,0,10,30)')

random

While this chart is far from real, but it is not TOO RANDOM and it has a trend.

fields

> field($name,$type,$special='database',$initValue=0,$min=PHP_INT_MIN,$max=PHP_INT_MAX)

fields are our values. They could be numeric, date and string.

  • Fields could have speed and acceleration. However, if you change the value manually, then the speed and acceleration is ignored.
  • "field.value=X" it sets the value of the field.
  • "field.speed=X" it sets the speed of the field.
  • "field.accel=X" it sets the acceleraiton of the field. If the speed is set manually, then the acceleration is ignored.
  • "field.stop=X" it sets the speed and acceleration to zero, and sets the value to X.

Examples: > "set field.value=20" // it sets the value of the field to 20 > "set field.speed=3" // it sets the speed of the field by 3. The field increases the value every cycle by 3 > "set field.accel=1" // it sets the acceleration of the field by 1. The field increases the speed every cycle by 1 > "set field.value=30 and field.speed=1" // the value is always 30, no matter the speed

->gen('when _index<40 then idtable.accel=1') // the acceleration is 1 until index=40
->gen('when _index<60 then idtable.accel=-1') // the acceleration is -1 until index=60
->gen('when _index<100 then idtable.accel=-1') // the acceleration is -1 until index=100

accel

gen

It generates a value using the Minilang syntaxis.

The syntaxis is as follow:

when logic and/or logic2 and/or logic3 then setvalue1 , setvalue2 , setvalue3

minilang

  • $var = it is php global variable.
  • field = it is an minilang variable.
  • * field.value it is the value of a column (if any)
  • _index = indicates the current index (current number of row)
  • 20 , 20.50 = its a fixed value
  • "text",'text' = it is fixed string value
  • fn() = its a function

Limits. * Parenthesis are now allowed (unless it is defines a function). > a1=(20+30) is not allowed however a1=20 , a1+30 is allowed * The syntax is divided in two parts, one of the logic (when) and other is the set (then) * Conditions are evaluated in order. If one condition is meet, then it stop other evaluations. > when a1=1 then b1=1 > when a1<10 then b1=10 // if a1=1 then this expression is not evaluated.

Logic

It sets the condition(s) separated by and or or

> It is not allowed to use more than operator for logic. a1=20+30 is not allowed.

Range Functions (numbers)

Functions that generates a range of values

ramp($fromX, $toX, $fromY, $toY)

It generates a ramp values (lineal values)

->gen('when _index<200 then idtable.value=ramp(0,100,10,1000)')

ramp

->gen('when _index<200 then idtable.value=ramp(0,100,1000,10)')

ramp inverse

log($startX,$startY,$scale=1)

It generates log values

> ->gen('when _index<200 then idtable.value=log(0,0,100)')

log

exp($startX,$startY,$scale=1)

It generates exponential values. The scale is for division of Y

> ->gen('when _index<200 then idtable.value=exp(0,0,10)')

exp

sin($startX,$startY,$speed=1,$scale=1)

It generates a sinuzoid values. The angle is calculated with the current index x the speed (in degree)

> ->gen('when _index<200 then idtable.value=sin(0,0,1,1)')

sin1

> ->gen('when _index<200 then idtable.value=sin(0,0,10,1)')

sin10

atan($centerX,$startY,$speed=1,$scale=1)

It generates arc-tangent values

->gen('when _index<200 then idtable.value=atan(50,0,20,10)')

atan

parabola($centerX,$startY,$scaleA=1,$scaleB=1,$scale=1)

It generates a parabola. It is possible to invert the parabola by changing the scaleA by negative

> ->gen('when _index<200 then idtable.value=parabola(50,0,1,1,1)')

parabola1

> ->gen('when _index<200 then idtable.value=parabola(50,0,-1,1,1)')

parabola2

> ->gen('when _index<200 then idtable.value=parabola(50,2500,-1,1,1)')

parabola3

bell($centerX, $startY, $sigma=1, $scaleY=1)

It generates a bell values, sigma is the "wide" of the bell.

> ->gen('when _index<=360 then idtable.value=bell(50,0,30,100)')

bell30

> ->gen('when _index<=360 then idtable.value=bell(50,0,30,100)')

bell1

Fixed functions (numbers)

Functions that generates a single value

randomprop(...$args)

It generates a random value by using different probabilities.

> randomprop(1,2,3,30,50,20)

  • there is 30% for 1
  • there is 50% for 2
  • there is 20% for 3

> ->gen('when _index<200 then idtable.value=randomprop(1,2,3,30,50,20)')

randomprop

random($from,$to,$jump=1,$prob0=null,$prob1=null,$prob2=null)

It generates a random value from $from to $to.

random(1,10) // 1,2,3,4,5,6,7,8,9,10
random(1,10,2) // 1,3,5,7,9

> ->gen('when _index<200 then idtable.value=random(-10,10,0.2)')

random

Optionally, you could add a probability for each segment.

> random(0,100,1,10,20,70)

  • There is a 10% probability the random value is between 0 and 33
  • There is a 20% probability the random value is between 34 and 66
  • There is a 70% probabitity the random value is between 67 to 100

> idtable.value=random(0,200,1,80,10,10)

random trend lower

> idtable.value=random(0,200,1,10,80,10)

random trend middle

> idtable.value=random(0,200,1,10,10,80)

random trend higher

field.speed=xxxx

It sets the speed of the field.

field.accel=xxxx

It sets the acceleration of a field.

field.value=xxxx

It sets the value of a field

field.getvalue

It returns the value of a field

field.valueabs

It transform the value of a field to absolute (always positive)

field.day , field.month , field.year, field.hour, field.minute, field.weekday

It returns the current part of the date (day,month,year,hour,minute and weekday).

> It is used for field of the type datetime. > It is not for set, no matter the value.

field.stop=xxxx

It sets or returns the value of a field, it also marks the speed and acceleration to zero.

field.add=xxxx

It adds a value to a field. If the field is of the type datetime, then you could add an "hour","minute" and "day"

field.add=20 // add 20 to the field
field.add="5h" // adds 5 hours to the field. 5m = 5 minutes, 5d = 5 days

field.concat=xxxx

It concatenates a value to a field.

field.concat=20 
field.concat="abc" 

field.skip=xxx

It skips a value to the next value. It is used for date.

field.skip='day'  // it skips to the next day (it start at 00:00)
field.skip='month'  // it skips to the next month (it start at the first day of the month 00:00)
field.skip='monday' // (or the name of the day), it skips to the next monday (00:00 hours)
field.skip='hour' // it skips to the next hour (minute 00)

Arrays and texts

->setArray('arrayname',[])

It sets an array. If the array is associative, then the value is the probability of selection.

->setArray('arrayname',['a','b','c']) // it sets an array with 3 values with the same chances.

->setArray('arrayname',['a'=>80,'b'=>10,'c'=>10]) // it sets an array with 3 values with the changes of a(80%),b(10%) and c(10%)

->setFormat('formatName',[])

It sets a format (template) to merge different arrays. The arrays are marked as {{name-of-the-array}}. If the array is not defined then it returns the value of a field.

If the array is associative then it returns a value according it's probability.

->setFormat('maleNameFormats',['{{namearr}} {{lastnamearr}}','Dr.{{namearr}} {{lastnamearr}}'])

->setFormat('maleNameFormats',['{{namearr}} {{lastnamearr}}'=>80,'Dr.{{namearr}} {{lastnamearr}}'=>20]) //probability of 80% and 20%

randomarray("arrayname",'field'=null)

it returns a random row inside of the array declared with setArray(). If the array is associative then it returns a value according it's probability.

If the array is a list of objects, then it returns the value of the field.

->gen('when always set name.value=randomarray("arrayname")')

randomformat($nameFormat)

It generates a random text using a mixes of format and different arrays.

->gen('when always set fullname.value=randomformat("nameFormat")')

randomtext($starting,$arrayName,$paragraph,$wordMinimum,$wordMaximum)

It generates a random text by using the array with name $arrayName. The text could starts with a default text.

If $paragraph is not 0, then it could generates paragraph (line carriage)

->gen('when always then text.value=randomtext("Lorem ipsum dolor","loremIpsumArray",1,4,30)')

randommask($mask,$arrayName='')

It generates a text based on a mask

  • \# = a (optional) random digit
  • 0 = a random digit.
  • u = An uppercase letter
  • l = A lowercase letter
  • X = An optional letter (uppercase)
  • x = An optional letter (lowercase)
  • ? = A random word of the array
  • \ = Escape character, (the next character will be returned without processing)

> randommask("##-00 uu ll \0 - oo (a)","lastName") // where lastName is an array

version

  • 1.2 Some cleanup.
  • 1.1 Now Minilib is a external library
  • 1.0 First open source version
  Files folder image Files  
File Role Description
Files folder imagedocs (27 files)
Files folder imageexercise (5 files)
Files folder imagelib (2 files, 1 directory)
Files folder imagetests (2 files)
Accessible without login Plain text file .travis.yml Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  docs  
File Role Description
  Accessible without login Image file accel.jpg Icon Icon image
  Accessible without login Image file atan.jpg Icon Icon image
  Accessible without login Image file bell1.jpg Icon Icon image
  Accessible without login Image file bell30.jpg Icon Icon image
  Accessible without login Image file count.jpg Icon Icon image
  Accessible without login Image file dummychart.jpg Icon Icon image
  Accessible without login Image file empty.jpg Icon Icon image
  Accessible without login Image file examplecount.jpg Icon Icon image
  Accessible without login Image file examplesum.jpg Icon Icon image
  Accessible without login Image file exampleweekday.jpg Icon Icon image
  Accessible without login Image file exp.jpg Icon Icon image
  Accessible without login Image file log.jpg Icon Icon image
  Accessible without login Image file parabola1.jpg Icon Icon image
  Accessible without login Image file parabola2.jpg Icon Icon image
  Accessible without login Image file parabola3.jpg Icon Icon image
  Accessible without login Image file ramp.jpg Icon Icon image
  Accessible without login Image file ramp2.jpg Icon Icon image
  Accessible without login Image file random.jpg Icon Icon image
  Accessible without login Image file random101080.jpg Icon Icon image
  Accessible without login Image file random108010.jpg Icon Icon image
  Accessible without login Image file random801010.jpg Icon Icon image
  Accessible without login Image file randomprop.jpg Icon Icon image
  Accessible without login Image file randomsin.jpg Icon Icon image
  Accessible without login Image file random_.jpg Icon Icon image
  Accessible without login Image file sin1.jpg Icon Icon image
  Accessible without login Image file sin10.jpg Icon Icon image
  Accessible without login Image file sin1030.jpg Icon Icon image

  Files folder image Files  /  exercise  
File Role Description
  Plain text file example1.php Class Class source
  Accessible without login Plain text file example2.php Example Example script
  Accessible without login Plain text file examplearray.php Example Example script
  Accessible without login Plain text file exampledb.php Example Example script
  Accessible without login Plain text file exampletime.php Example Example script

  Files folder image Files  /  lib  
File Role Description
Files folder imageen_US (2 files)
  Plain text file ChaosField.php Class Class source
  Plain text file ChaosMachineOne.php Class Class source

  Files folder image Files  /  lib  /  en_US  
File Role Description
  Plain text file Person.php Class Class source
  Plain text file Products.php Class Class source

  Files folder image Files  /  tests  
File Role Description
  Plain text file AbstractStateMachineOneTestCase.php Class Class source
  Plain text file CompilationTest.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:57
This week:1
All time:9,461
This week:382Up