# Getting Text Metrics in Firemonkey

Firemonkey’s abstract TCanvas class has been providing the dimensions of the bounding rectangle of some text on itself. On recent updates it has deprecated providing text rectangle directly in the canvas, in stead encouraged users to use TTextLayout abstract class which handles several text-based operations on the native context of the platforms. So it is possible to get the bounding rectangle of a string on any canvas. Is this enough? Though I am sure this class will improve to involve many new textual features, at current time it lacks some major needs of a developer who designs text drawing applications.

When words in different sizes aligned from top you can’t get a proper sentence view

For the first time, I have faced a feature lack when I saw even TMS’s HTML Text was unable to align words in an HTML notation that have got different sizes. Because having text rectangle is not enough to locate words in different sizes because of the missing baseline information. If you align the words from the top the smaller word’s baseline will be upper (as in the above image), if you align the bottoms the baseline will be lower, so to make a correct match you should know where are the baselines of each word exactly located. So we need to know the Ascent value of the text. If have got the ascent value, we can easily align the words on the baseline as seen on following image. For a complete information on typography see this article on Wikipedia.

For a proper drawing of words in different sizes you should align them on their baselines.

As seen on the above image, having the information of ascent values for each word we can align them properly on the baseline of the sentence. However FMX TTextLayout doesn’t provide this information. So I decided to add a new function to my PlatformExtensions unit to provide metrics information that are gathered from the drawing of that specific font in a specific size. The new function is GetTextMetrics and hast this following structure.


Class Procedure GetTextMetrics(Text:String; Font:TFont; var TextRect:TRectF;
var Ascent,Descent:Single;
var CapHeight,XHeight:Single);virtual;abstract;



## Getting Text Metrics in Windows

Firemonkey uses two different context types on Windows platform that are Windows GDIP and Direct2D. The default one is Windows GDI plus context which is a set of device independent drawing API based on the old GDI. Because of its device independency GDIP lacks of some important drawing features such as XOR brush, but it is always possible to handle these extra operations by locking the context to device dependent HDC and doing things on the device bitmap. What I mean is; getting font metrics is not as easy as it was old GDI based on HDC, so I used the limited information of the GDIP and estimated some others like (x height  of text) by using statistical multipliers. However someone other can collapse the context to HDC level and calculate more detailed font metrics on that level. I didn’t prefer it to be consistent with the design logic of GDIP, and used TGPFontFamily class of GDIP to get the information about the font. Note that the metrics values gathered from this class is in logical units so should be converted to pixels using the proportions.

Here are my calculations:

fSize := FGPFont.GetHeight(FGraphics);
cAscent := FGpFamily.GetCellAscent(FontStyle);
cDescent := FGpFamily.GetCellDescent(FontStyle);
emHeight := FGpFamily.GetEmHeight(FontStyle);
lSpacing := FGPFamily.GetLineSpacing(FontStyle);
Ascent := fSize * (cAscent)/lSpacing;
Descent := fSize * (descent)/lSpacing;
XHeight :=  fsize * (cAscent-iLeading) *(7/10) /lSpacing;


Note that the calculations use TGPFont, TGPFontFamily and TGPGraphics classes to deal with font metrics. The FGPFont and FGPGraphics objects are not accessible through standard class hierarchy, however we can use RTTI functions to access them.


RttiField := RttiContext.GetType(Layout.ClassType).GetField('FGPFont');
if assigned(RttiField) then
begin
FGPFont := TGpFont(RttiField.GetValue(Layout).AsObject);
RttiField := RttiContext.GetType(Layout.ClassType).GetField('FGraphics');
FGraphics := TGpGraphics(RttiField.GetValue(Layout).AsObject);

FGPFamily := TGPFontFamily.Create;
FGPFont.GetFamily(FGPFamily);
// Get Metrics
// ...
end;


Another point to be mentioned here is that, above calculations work only if the DefaultCanvas is a GDIP canvas. If the developer selects to use the D2D canvas, because of the absence of the FGPFont object in RTTI the text metrics will not be updated. Since I don’t have any experience on Direct2D I haven’t worked on it, but I will be happy if someone does it and send me so that I can put it in the class code by giving credits to his/her name.

## Getting Text Metrics in Mac OS X

Things are always easier for programmers in Mac side. You can see below how it is easy getting font metrics using CoreText API of Cocoa.

LFontRef := CTFontCreateWithName(CFSTR(Layout.Font.Family), Layout.Font.Size, nil);
Ascent := CTFontGetAscent(LFontRef);
Descent := CTFontGetDescent(LFontRef);
CapHeight := CTFontGetCapHeight(LFontRef);
XHeight := CTFontGetXHeight(LFontRef);
CFRelease(LFontRef);


You can see all the details of using TTextLayout class and how to extend it to get Text/Font Metrics in my PlatformExtensions units. You can get the source code of the PlatformExtensions classes with the demo application from this SVN link. For non-programmers the compiled Win32, Win64, MacOSX (Thanks to Firemonkey) applications are also available to download.

# Getting List of Running Applications in Firemonkey for Windows and OSX

A common need for programmers is to prevent running of the application more than once. For this purpose we mainly use mutex in Windows programming and close the application if an already active mutex is in memory. In Firemonkey applications this approach is not valid in OSX side as it is in Windows. So the first alternative way that comes to mind is to search for all running applications at start-up and close the application if it has got two (2) instances. So the question is how we will get the list of running applications? We will need this also for Windows as well as OSX, because doing the things using the same strategy for both Windows and OSX is a common pattern in Firemonkey.

As you can see in one of my earlier articles, I have started a  new class named TPlatformExtensions, to host these kind of extra utilities which will expand platform specific usage. So I will add a new function to this class to solve this specific platform related problem.

### Getting the Running Applications list in OSX

On OSX api, NSWorkspace class is used to deal with issues about the workspace that the application is hosted. And this class has got two selectors (methods) that is related with the subject.

- (NSArray *)launchedApplications
- (NSArray *)runningApplications


Both functions can be used to get the list of running Applications, however the first one is a deprecated one. So the second one should be used to get rid of future problems about compatibility, bu this function is not fully supported in Delphi port. Because this functions returns an array of NSRunningApplication objects which originally we can get any information about the application instance. However, in Firemonkey port this class includes a very brief list of methods which we can only learn the processId of the application that wont mean anything for us to check the double instantiation. So I decided to use the deprecated function hoping that soon I will replace it with the second one  after a possible hot-fix enriching the NSRunningApplication interface.

To be able to use workspace instance methods first we should have got an instance of our workspace. So we will call the

+ (NSWorkspace *)sharedWorkspace


class method to get the shared workspace. The launchedApplications methods returns an NSArray object which includes the list of NSDictionary objects that each of them represents a bundle of information of each running application. So we should typecast the items in the list to NSDictionary and get the values of the keys we know. Possible keys that we can use to get information about the application are NSApplicationBundleIdentifier, NSApplicationPath and NSApplicationName. Here how we do all in Delphi side.

class procedure TPlatformExtensionsMac.GetRunningAplications(Applist: TStringlist);
var
fWorkSpace:NSWorkSpace;
list:NSArray;
i: Integer;
lItem:NSDictionary;
key,value: NSString;
begin
fWorkSpace := TNsWorkspace.Wrap(TNsWorkSpace.OCClass.sharedWorkspace);
list := fWorkspace.launchedApplications;
if (List <> nil) and (List.count > 0) then
begin
for i := 0 to list.count-1 do
begin
lItem := TNSDictionary.Wrap(List.objectAtIndex(i));
key := NSSTR(String(PAnsiChar(UTF8Encode('NSApplicationBundleIdentifier'))));
// You can also use NSApplicationPath or NSApplicationName
value := TNSString.Wrap(lItem.valueForKey(key));
end;
end;
end;


### Getting the Running Applications list in Windows (32bit and 64bit)

In windows though the standard api introduced in Windows.pas doesn’t provide a support for getting the list of running applications, Microsoft has got a support in kernel32.dll called tool help library and fortunately Delphi has got a port of this group of functions under the name Winapi.TlHelp32.pas. So we will use this library to get the list of running applications by searching the heap with given functions. Don’t be mistaken by the name. It is used both in 32 and 64 bit modes. Here you can see how it is also easy to get the running applications list in Windows.

class procedure TPlatformExtensionsWin.GetRunningAplications(Applist: TStringlist);
var
PE: TProcessEntry32;
Snap: THandle;
fName: String;
begin
pe.dwsize:=sizeof(PE);
Snap:= CreateToolHelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if Snap <> 0 then
begin
if Process32First(Snap, PE) then
begin
fName := String(PE.szExeFile);
while Process32Next(Snap, PE) do
begin
fName := String(PE.szExeFile);
end;
end;
CloseHandle(Snap);
end;
end;


You can get the source code of the PlatformExtensions classes with the demo application from this SVN link. For non-programmers the compiled Win32, Win64, MacOSX (Thanks to Firemonkey) applications are also available to download.

# TVariantColumn for Firemonkey Grid: Different Cell Types in the Same Column

Everything started when I should make a property editor using the Firemonkey TGrid. After my searches on the net, I have seen that no working code for this is provided but only some code which shows how to make your own column which is for a different cell type. But in this case, I needed a grid which I can host different types of cells in one column. I have seen that it’s not  that easy so I designed a new column class TVariantColumn which can host different cell types in a performant way. Here is the story and the code.

Firemonkey grid is a powerful one which has some major differences as it was in VCL. The grid doesn’t have got its own render engine for cells, instead it hosts other controls in the cells and these controls all make the drawing themselves. These controls show their data not from the grid but from our own data objects using the OnGetData event. To be able to make all these in a performant way only the controls for the visible cells are created. The responsible code for this is in TColum class which is the proxy for cells,  it creates the cell control (say TEdit) when it is needed and locates it on the grid. Each column can create cells of its type. The cell controls are reserved in an array, so when the amount of the visible rows exceed, more controls are created. So any control in the array doesn’t belong to a specific row, it can be showed in any row when it is visible.

Now let us think, what we should do if we want to show a different cell type in each row of a column. According to the current TColumn design, this is not possible. Because the controls in the array aren’t for specific rows, but any visible row in the column.  So we should change the cell update logic of the TColumn, which will not directly create the control but make some other class create the control which is responsible for that kind of cell. The new logic will be handled in our new column class TVariantColumn and the TCellProxy class with its descendants will be inter proxies that will be responsible for the cells of one kind.

### TVariantColumn Class

This class is inherited from TColumn class, and what is mostly changed is the logic in UpdateCells. This function calls the CreateCellControl for each row in the visible area. The CreateCellControl doesn’t create the cells itself but first finds out which proxy should be responsible for the cell (through event) and then asks the proxy to create the cell. The proxy checks its que when asked, if there is any non-reserved cell it gives it directly otherwise create a new one. So the possible amount of cells in one proxy is the possible number of that type of cells visible together in the grid. All these makes the control creation and usage performant.

To use this class, just create it and add it to your grid in runtime when you need it. Before adding to the grid be sure that you set the OnGetCellProxyIndex event property.


vColumn := TVariantColumn.Create(Self);
vColumn.OnGetCellProxyIndex := GetCellProxyIndex;



After this, you will need to create the proxies for each type you will use in this column. We have got 13 type of proxies that are all inherited from TCellProxy.

1. TTextProxy
2. TCheckProxy
3. TSwitchProxy
5. TProgressProxy
6. TPopupProxy
7. TComboProxy
8. TColorComboProxy
9. TComboColorProxy
10. TImageProxy
11. TDateProxy
12. TTrackbarProxy
13. TCustomProxy

When you create a cell proxy like

txProxy:= vColumn.NewCellProxy(TTextProxy);


the proxy will be created by the vColumn using the class type passed and will be added to its list. The first proxy created will have an index 0 and the second will have 1 and goes on. So you should give this index for a specific row when asked by OnGetCellProxyIndex event.

The list includes almost all types of editors that is available as standalone in Firemonkey.  I will code a few more proxies in near future like for example TComboTrackbar control, but the good news is that you can use any other control using the TCustomProxy. With this class, the controls are created and handled in your own code through the events. All of the cell proxies use a cell type class inherited from the native control class like TEdit, TCombobox. The same strategy is used in TColumn and cell relation. These cell type classes use standard styling so when your stylebook uses a different style every cell in VariantColumn will obey the change.

If you want to have your own proxy, just inherit one from TCellProxy. You can  see how it’s done from my code. To get the source code of the FMX.VariantColumn.pas with the project source of demo application use this SVN link. The class code is in the Utils folder.  For non-programmers the compiled Win32, Win64, MacOSX (Thanks to Firemonkey) demo applications are also available to download.

# Getting System Fonts List in Firemonkey : TPlatformExtensions class

In stack-overflow forum which I make use of very much, someone asked this question. How can we get the list of installed fonts in Firemonkey as we have in VC TScreen.Fonts? For Firemonkey these kinds of questions mostly is a deal with cross-platform compatibility, so any kind of actions that is called directly for the core system should be done twice, one for Windows the other for OSX. However I strongly suggest to my readers and new programmers that always they should keep platform dependant code in separate units, ans use an abstract layer to combine them. A good example for this is the new TPlatformExtensions class which I have recently started. The first utility function of the class is GetSystemFonts which is a solution for the question in stack-overflow and for others who will need the list of the available fonts. This class will be an abstract class defined in FMX.PlatformExtensions.Pas and a Windows and OSX versions will be implemented in FMX.PlatformExtensions.Win.pas and FMX.PlatformExtensions.Mac.pas units separately.

So in this blog, I have a chance to show you both a specific and a general case together: Getting the list of the fonts in Firemonkey, and handling this cross-platform related issue in a reusable patterned way.

### TPlatformExtensions Class

Firemonkey has a got a TPlatform class which is a port to platform related issues. But of course it doesn’t handle all the things that were handled in VCL. This all because of time and the double man-power that should be used in the implementation. However enough know-how is also in the web to handle any platform related issue  so you can easily solve these kind of problems. I decided to collect all my platform dependant solutions in one class which I called TPlatformExtensions. For now, it has got only one utility function, but each time I design a platform related solution in this blog I will add it to this class. Here is the class:

TPlatformExtensions = class(TObject)
public
Class Procedure GetSystemFonts(FontList:TStringlist);virtual;abstract;
end;


This class is an abstract class ( means will have abstract methods to be overridden) and only the inherited platform classes will be created in runtime. But users will call the platform functions using this class. Son in the same unit there vill be a variable definition for users to use.

var
PlatformExtensions:TPlatformExtensions;


This is like using Printer object, which is behaves like a singleton of TPrinter class. So in the initialization block, this object will be created automatically according to the platform and will be freed when the unit is finalized.

initialization
{$IFDEF MACOS} PlatformExtensions := TPlatformExtensionsMac.Create; {$ENDIF}
{$IFDEF MSWINDOWS} PlatformExtensions := TPlatformExtensionsWin.Create; {$ENDIF}
finalization
PlatformExtensions.Free;


Note that we have two other class names here, one of the is for Mac and the other is for Win. They can be reached in their units which is referenced in a uses clause after implementation to get rid of circular references.

uses
{$IFDEF MACOS} FMX.PlatformExtensions.Mac; {$ENDIF}
{$IFDEF MSWINDOWS} FMX.PlatformExtensions.Win; {$ENDIF}


So in its simplest form, just add the FMX.PlatformExtensions in your own uses clause and use PlatfromExtensions objects.

Here are the two PlatformExtensions class definitions for Win and Mac.

Declared in PlatformExtensions.Win.Pas

 TPlatformExtensionsWin = class(TPlatformExtensions)
public
Class Procedure GetSystemFonts(FontList:TStringlist);override;
end;


Declared in PlatformExtensions.Mac.Pas

 TPlatformExtensionsMac = class(TPlatformExtensions)
public
Class Procedure GetSystemFonts(FontList:TStringlist);override;
end;


### Coding the first procedure: GetSystemFonts

In OSX, the fonts list can be reached using the  sharedFontManager object of NSFontManager. It will return the font list as a NSString array. So you should first wrap a NSFontmanager object, then call the availableFontFamilies selector. The result is an NSArray which you can go through its list and get each family name as NSString. Note that an NSString is not a Delphi string, so you should convert it in someway. A cheap and practical way is to use the UTF8 characters which Delphi directly can cast to String type. For example;

MyDelphiString := String(MyNsString.UTF8String);


So here is the procedure code for GetSystemFonts in Mac platform:

class procedure TPlatformExtensionsMac.GetSystemFonts(FontList: TStringlist);
var
fManager: NsFontManager;
list:NSArray;
lItem:NSString;
i: Integer;
begin
fManager := TNsFontManager.Wrap(TNsFontManager.OCClass.sharedFontManager);
list := fManager.availableFontFamilies;
if (List <> nil) and (List.count > 0) then
begin
for i := 0 to List.Count-1 do
begin
lItem := TNSString.Wrap(List.objectAtIndex(i));
end;
end;
end;


Windows SDK coding is a little bit more complicated as it has been before. To be able to get the system fonts one should use the EnumFontFamiliesEx api function which uses a callback function to enumerate each font family in the system. I have been inspired by the TScreen.GetFonts in VCL code, and here it is:

function EnumFontsList(var LogFont: TLogFont; var TextMetric: TTextMetric;
FontType: Integer; Data: Pointer): Integer; stdcall;
var
List: TStrings;
fName: string;
begin
List := TStrings(Data);
fName := LogFont.lfFaceName;
if (List.Count = 0) or (AnsiCompareText(List[List.Count-1], fName) <> 0) then
Result := 1;
end;

class procedure TPlatformExtensionsWin.GetSystemFonts(FontList: TStringlist);
var
dContext: HDC;
LFont: TLogFont;
begin
dContext := GetDC(0);
FillChar(LFont, sizeof(LFont), 0);
LFont.lfCharset := DEFAULT_CHARSET;
EnumFontFamiliesEx(dContext, LFont, @EnumFontsList, Winapi.Windows.LPARAM(FontList), 0);
ReleaseDC(0, dContext);
end;


You can get the source code of the PlatformExtensions classes with the demo application from this SVN link. For non-programmers the compiled Win32, Win64, MacOSX (Thanks to Firemonkey) applications are also available to download.

# Making a Pipe with bend feature using Delphi Firemonkey

The cross-platform Firemonkey framework of Delphi introduced 3D support which is based on both OpenGL and DirectX.  Although it’s not as rich as GLScene (an open source Delphi 3D Components Library), for me it is a better way to deal with 3D if you are making a business application. Because it is simpler and has got a more traceable code path to review. I have used GLScene in some of my previous projects but I liked Firemonkey 3D more. Maybe this is about I am not a game programmer and only use 3D for CAD and business applications but if I would write a game application I would again prefer Firemonkey 3D instead of GLScene VCL. Anyway, if you also like Firemonkey 3D as me, you also have a problem. Firemonkey doesn’t have enough components to make a high level 3D applications. But who cares? You have a lot of ways to make your own 3D components, and I am sure in near feature there will be a lot of programmers who will publish rich components for Firemonkey on their blogs. In one of my projects I needed a pipe object, which includes an inner cylinder, and outer cylinder and two annulus on top and bottom. I could use standard cylinder object but it would not be enough for me. First reason for this was, the top and bottom would remain as circles but I needed annulus shape, secondly when I want to modify the meshes I would need my own mesh math. So I decided to make a brand new TPipe (and also TAnnulus) object, which you can also modify it with bend, twist and emboss modifiers. I liked what I have at the end, I don’t say it is the best pipe mesh on the net,  but I am sure you will not have a better one until near feature.

### What is a Pipe?

A pipe in geometry is a real-world shape like cylinder. An absolute cylinder shape has not got a thickness, if you put a thinner cylinder inside a thicker one, you will simply get a pipe with thickness of radius difference. As you may have thought this object will have got special top/bottom closes. It will not be a circular disk but it will be a an annulus shape.

Annulus Geometry. Source: Wikipedia

So a pipe is in fact an extrude of an annulus. It seems there is an easy way to have a pipe in Firemonkey. Make an annulus shaped polygon, and extrude it with proper calls.  Is that easy?

### How to make a Pipe in Firemonkey.

The answer is no. It is not as easy as extruding an annulus because you can’t modify the vertices of an extruded shape. For example if you want to bend your pipe to a specific angle, then you should modify the vertices. So in Firemonkey the best approach is using a meshed object which the TCube, TCylinder, etc. also use. Firemonkey has got a TCustomMesh class which you can inherit any new meshed object. Building meshes is very easy if you know the math behind it. So in my TAnnulus and TPipe I have used the TCustomMesh class as the base class, rebuild the mesh according to my own geometry and Firemonkey has done the rest.

The pipe is laid through the Y axis. So the Width and Depth defines the a,b diameters of the elliptical annulus (it is a circular when Width and Depth is equal).  The Height value defines the length of the cylindrical side on Y axis. The inner circle (or ellipse) is calculated from Width and Depth parameters using the Thickness property.

#### Having a Rectangular Shape

A pipe is a cylinder-like object so an annulus is circle-like one. But why don’t we try to think the overall shape as rectangular. The math is not so different so I also added the rectangular shape to my annulus and pipe.You can define if the inner, outer or both frames as rectangular or elliptical.

#### Having a Section On Pipe

Sometimes we may need to see the inside of a pipe by making angular sections on it. I have handled this case by adding a section feature in my pipe. You can define the section in any magnitude using the SectionAngle property and you can locate the section on top or bottom side using the SectionType property. (sctNone, sctTop, sctBottom). The section is on X,Y plane.

### The Pipe Modifiers

A pipe modifier is a specific class (TPipeModifier) which takes the circular (or rectangular) X,Z sections of the pipe a TPointLayer, and modifies them according to its own math. For instance if you want to make a thicker emboss on a specific place of pipe, then the modifier will scale the layers. If you want to bend the modifier will rotate the layers. All modifying is based on the X,Z section layers. A pipe modifier will need a start position and an end position. All positions are based on 0, which means the bottom start of the pipe, on its local Y = -Height/2. The modifiers also have got a Subdivisions property. Using this property the modification density will be arranged.

#### The Bend Modifier

This TBendModifier class is used to bend the pipe starting on a specific position to a specific position using a specific angle and subdivisions. If the total length of the bend (EndPosition – StartPosition) is bigger the bend radius will be bigger. The bend is arranged with BendAngle. When following the / axis for bottom to top, a positive bend will mean a left side bend, while a negative bend will mean a right side bend. If you want the bend move also on Z axis you should use the TurnAngle property.

Bend Figure 1

In the above figure (1)  the pipe has a length value 8. The bend starts on 1.0 and ends on 2.5. The bend angle is 90 degrees so it is on the left. In the below figure (2) a second bend is added on the position 2.5. It ends on the position 4.It’s angle is -90, so it is a turn to right.

Bend Figure 2

Now if you want to turn the bend on Z axis we will use the TurnAngle property. In the figure below (3) the first bend has got an extra TurnAngle value of 90 with its 90 degrees bend angle, and the second is same.

#### The Emboss Modifier

Using the emboss modifier, you can make any part of the pipe bigger or smaller. Starting from the StartPosition and ending on EndPosition the section of the pipe is scaled with the thickness ratio of the modifier. The thickness ration is 0.1 by default. To make a smaller section use a negative value. The margins are 0.02 by default, but if you want softer ends you can use a little bigger value like 0.1 or 0.2.

### The Twist Modifier

This a bonus modifier. 🙂 ) . Normally it is not a commonly used effect in business applications but you can use it to see what you can do with modifiers.

For downloading the full source code of the demo project with the FMX.MeshObjects unit which includes the TAnnulus, TPipe and modifiers, you can use this SVN link. For non-programmers the compiled Win32, Win64, MacOSX (Thanks to Firemonkey) applications are also available to download.

# Understanding the nature of Randomness and Testing Delphi’s Random function

Randomness is a key concept in Science and Philosophy. It has got a mathematical form, physical behaviour and philosophical mystery. I have studied this concept in my MD thesis under the title; “The Randomness Problem in 20th Century Thought”. I have come up several scientific and philosophical results, however I want to discuss a particular aspect of the problem, the behavioral form (physical mathematics) of randomness here.

The random distributions (random sets) have their own nature, they tend to obey a statistical discipline if it is really natural. In computer science there is a strong distinction between the true random numbers and the pseudo random numbers. The true ones come from nature as the result of a dice playing and the pseudo ones are results of logical/mathematical processes. So mostly the ones generated in computers are pseudo numbers as a result of a mathematical formula. True randomness have got an order, if the items in the random sets have got an equal chance of existence, the share also must be close to equal with a minor error. This error gets even smaller when the set grows. Think of a man throwing a coin. If he throws twice, the result may be one of this patterns; [TH, HT, TT, HH]. In this result set, Head and Tail shares the existence equally in a 50% possibility. So in %50 of the cases the share is not equal; and that means the error is %50. Is it really minor? 🙂 So you can see here that if we make more throwing the possibility of error will get smaller. You can calculate the quantity of the error with the $\frac{\sqrt{n}}{2}$ formula where n is the number of throwing.

When you calculate the error for each n and plot it on a graph, you will see a well-know statistical pattern which is called the normal curve. The ideal normal curve can be drawn with the formula $\frac{1}{\sqrt{2\pi}}.e^{-x^{2}/2}$.

This normal curve the behavioural pattern of the nature. If you throw dice million times you can draw this graph. This graph is neither physical not mathematical. It means there is no rational base for the normal curve either in math or physics. Mark Kac, an important modern mathematician, makes a methodic discussion on the mystery of the behavioral pattern of random sets in his article about randomness problem[1].He declares that a random set is true if only it behaves according to this normal curve pattern and suggests a coin toss experiment with huge numbers to be able to see the graph as a result.

So in this article; we will follow Mark Kac, and make experiments to see if random generators arround us are natural or not. The most nobel way to test randomness is to observe particle behaviours under atomic level which is the ultimate kingdom of randomness. An easier but not easy way is to throw dice million and million times to see the graph. As a computer programmer I will chouse my easy way; programming in Delphi, both to see the natural randomness and Delphi’s randomness. To test randomness in its natural way, I can’t trust un the Delphi’s Random function (or any computer’s) as it is stated in the Delphi’s reference, so I need physical data from the nature itself. For this I will use the random.org website which generates random numbers using the atmospherical noise.

First of all I will code a class named TOnlineRandomizer which will connect to Random.org url based service and get random numbers. Note that Random.org free web services are limited by generated bits, however the free bits are enough to get an observable result.  Here is the class header for TOnlineRandomizer.

TOnlineRandomizer = class(TObject)
private
FResultList: TStringList;
FIntUrlPattern:String;
FDecUrlPattern:String;
FSettings:TFormatSettings;
Procedure GetResultsFromWeb(qUrl:String);
function GetRandomInteger(Index: Integer): Integer;
function GetRandomDecimal(Index: Integer): Single;
function GetCount: Integer;
function GetRandomString(Index: Integer): String;
public
Constructor Create;
Destructor Destroy;override;
Procedure GetRandomIntegers(iMin,iMax,iCount:Integer);
Procedure GetRandomDecimals(dPrecission,dCount:Integer);
Procedure GetPseudoRandomIntegers(iMin,iMax,iCount:Integer);
Procedure GetPseudoRandomDecimals(dPrecission,dCount:Integer);
end;


The class has got to execution procedures, GetRandomIntegers which calls the integer generator service of Random.org, and GetRandomDecimals which calls the fraction generator service of Random.Org. After the calling any of the execution you can reach the results using the array properties, RandomInteger to get the item as integer, RandomDecimal to get the item as float and RandomString to get the item as string. In both generation functions you should give a count, and after the results the readonly count property will equal to your count request or less in case of any limitation. The results are received from Random.org as text file, so they are stored in a TStringlist, you should know that reaching the numbers through RandomInteger or RandomDecimal property will cost an additional conversion time and risk, so if you will just show the numbers on the screen you can use the RandomString property which gives the received item from the list with no conversion.

A typical usage of the class may be like this.

procedure TForm1.Button1Click(Sender: TObject);
var rndGen:TOnlineRandomizer;
i: Integer;
begin
rndGen := TOnlineRandomizer.Create;
rndGen.GetRandomDecimals(20,100);
for i  := 0 to rndGen.Count-1 do
rndGen.Free;
end;


The TOnlineRandomizer class has got a private method GetResultsFromWeb, in which it receives the numbers from Random.org using the formatted url with the parameters of GetRandomIntegers/GetRandomDecimals. The results are CR+LF separated so can be directly loaded to a TStringList – FResultList which is created in the constructor, destroyed in the destructor and will store the random numbers during the session. The url formats are arranged according to the interface of Random.Org, but you can direct to request to any other online randomizer service by changing the url formats in the constructor.

constructor TOnlineRandomizer.Create;
begin
inherited Create;
FIntUrlPattern := 'http://www.random.org/integers/'+
'?min=%d&max=%d&num=%d&col=1&base=10&format=plain&rnd=new';
FDecUrlPattern := 'http://www.random.org/decimal-fractions/'+
'?dec=%d&num=%d&col=1&format=plain&rnd=new';
FResultList := TSTringList.Create;
FSettings := TFormatSettings.Create;
FSettings.DecimalSeparator := '.';
end;

destructor TOnlineRandomizer.Destroy;
begin
FResultList.Free;
inherited;
end;

procedure TOnlineRandomizer.GetRandomDecimals(dPrecission, dCount: Integer);
var qUrl: String;
begin
qUrl := Format(FDecUrlPattern,[dPrecission, dCount]);
Self.GetResultsFromWeb(qUrl);
end;

procedure TOnlineRandomizer.GetRandomIntegers(iMin, iMax, iCount: Integer);
var qUrl: String;
begin
qUrl := Format(FIntUrlPattern,[iMin,iMax,iCount]);
Self.GetResultsFromWeb(qUrl);
end;

procedure TOnlineRandomizer.GetResultsFromWeb(qUrl: String);
var httpObj:TIdHttp;
FResultStream: TMemoryStream;
begin
FResultStream := TMemoryStream.Create;
httpObj := TIdHttp.Create(nil);
httpObj.Get(qUrl,FResultStream);
FResultStream.Position := 0;
FResultStream.Free;
httpObj.Free;
end;


In addition to these main methods, we have some other methods for the read only properties as below.

function TOnlineRandomizer.GetCount: Integer;
begin
Result := FResultList.Count;
end;

function TOnlineRandomizer.GetRandomDecimal(Index: Integer): Single;
begin
result := StrToFloat(FResultList[Index],fSettings);
end;

function TOnlineRandomizer.GetRandomInteger(Index: Integer): Integer;
begin
result := StrToInt(FResultList[Index]);
end;

function TOnlineRandomizer.GetRandomString(Index: Integer): String;
begin
Result := FresultList[Index];
end;


In addition to the online generated true random numbers, we will also generate pseudo random numbers for comparison. For this purpose, I have added tow additional functions to generate integer or decimals through the Delphi’s random function.

procedure TOnlineRandomizer.GetPseudoRandomDecimals(dPrecission,
dCount: Integer);
var
I: Integer;
rndDec:Single;
begin
Randomize;
for I := 1 to dCount do
begin
rndDec :=   Random;
end;
end;

procedure TOnlineRandomizer.GetPseudoRandomIntegers(iMin, iMax,
iCount: Integer);
var
I,rndInt: Integer;
begin
Randomize;
for I := 1 to iCount do
begin
rndInt := Random(iMax-iMin+1)+iMin;
end;
end;


Time For Test
Our randomizer class is ready, both for true and pseudo random number generations. Now, we need an application which we can get the random numbers through our class, analyze them with some statistical math and draw an error distribution graph to see the nature of random numbers. For this purpose I will use Delphi Firemonkey framework to collaborate all together.

One question here is about how we will analyze the random numbers. For this analyze, I will use the statistical grouping method. Assume that I have got 10 coins and throw them all. The most expected result is an equal distribution of Head and Tails, but it is not impossible to get extreme results like having all of them Heads or Tails. But the equal or close distribution of Head/Tails are more probable than the extreme results. For example; having all of them Head is equal to 1/ 210 probability and this means that I should start to hope seen this result after at least I throw the ten coin 1024 times. But more I throw, say a hundred thousand times, I should see all the possibilities distributed in the result set according to its probability. The exact figure of this result is normal curve which is stated as above. So what mathematical method should we use to get this graph?

We will throw the 10 coins as much as possible, or group the single tosses as to have 10 tosses in each. For each group we will count the heads and tails. If they are equal the error is 0, if heads are 1 more than the error is +1, if the tails are one more than the error is -1. This goes on. If all of them are heads the error is +5. So give an error point to each group, and regroup them according to their errors. Now you should count the number of groups for each error like How many of groups have +2 error. Plot the count of the groups for each error on the graph, so on th x-axis you will have 11 ticks, the middle one is for 0 error, left 5 ticks are for tail errors and right 5 ticks are for head errors. If this plot gives you a normal curve, t means that your numbers are true random numbers or pseudo numbers that have got a strong naturalness.

In the application that I called RandomNature, I used the above statistical method to analyze the random numbers. I have used 14 tosses for each group, but you can change this by just setting the errorStep variable in the Analyze function. After plotting the results on the graph, I fit them to a bezier curve using my own smoothing algorithm which I will describe in a future article. But it is in the code you can use it in any way.

According to the tests, I can say that the true numbers from Random.org works better than the machine generated ones especially in small amounts of tosses. Above is a result of both method, with a 10.000 total tosses.

The first result of a small set from Random.org. A 10.000 total tosses used.

The first result of a small set from Delphi’s Random function. A 10.000 of total tosses used.

As you can see from the graphs, the true numbers seem more natural, however when considering small sets, I have observed different results from these. Sometimes Delphi seemed more natural than the true numbers. This is because 715  tossing of 14 coins, doesnt give the natural results. It has an error potential of un-naturalness according to √n rule. So to be sure about the results we should make the error potential smaller using bigger amount of tosses. So lets look at the results of 100.000 coin tosses.

The result of 100.000 coin tosses from Random.org. It seems that Random.org numbers are really natural. To get a better normal curve results you should press the button more to get more random numbers.

The result of 100.000 coin tosses from Delphi’s Random function. It seems that Delphi’s randomness is not unnatural as it is expected . To get a better normal curve results you should press the button more to get more random numbers.

So as a result Delphi’s random function passed the test.

For downloading the full source code of the project with the OnlineRandomizer unit you can use this SVN link. For non-programmers the compiled Win32, Win64, MacOSX (Thanks to Firemonkey) applications are also available to download.

[1] Mark Kac, “What is random?”, American Scientist,v.71, pp.405

# Introduction To Neural Networks – Part 1: The Neuron

Neural Networks is an important subject in artificial intelligence to handle things in the way our brain does. Besides its importance in artificial intelligence, it provides a base for biological modelling of the  human (or animal) brain organization. Though it’s not the final conclusion about how the brain works, the model that neural networks suggest is a reasonable, practical, functional and working reduction of the brain.

The brain is a complex and huge connection of nerve cells called neuron, each neuron collects information (signals) from other neurons with dendrites, and after a simple calculation inside its soma it generates its own information (signal) and distributes it to other neurons through its axon. So from this sentence you can easily understand that the axon terminals connects with other dendrites. This connection is called a synapse.

The neuron as it is in the brain – Source : Carlson NA (1992) Foundations of Physiological Psychology. Needham Heights, Massachusetts: Simon & Schuster.

A synapse is the gate to transfer electro-chemical material from one cell to the other. This material can either downgrade (-) the soma load or upgrade (+) it. The synapses vary from each other by their effect which is called in AI terminology the weight. So each input signal enters to the soma is summed after it is multiplied by its weight. Now the cell has a load in itself. If this load is more than the cell can keep (greater than its threshold), a signal (1) will be generated through the axon independent of how much the load is greater than the threshold.

So if we want to simulate this neuron, we will just use this simple math. Take the inputs, multiply each of them with its weight, sum up all, check if the total is bigger than the threshold or not. If yes fire the signal (1), if No keep silence (0). It is simple as illustrated in below (Figure 1), but man could make the history real with this simple math in his brain.

Figure 1: Simple Math inside a Neuron

To make the simulation I will code an Object Pascal class in Delphi (Firemonkey) and then make a simple application to test it. Note that a single neuron is not so useful for doing complex things, for making real AI, you should connect many neurons each other and arrange the weights and thresholds. A code model for connecting neurons will be also published in this blog as Part 2.

Our class name is TNeuron. It has got an array property of type Single for inputs, an array property of type Single for weights, a Single property for threshold, a Single property for output. Note that even the input and output property may be either 0 or 1 (since it represents a digital signal), we will make their types as single (not Byte, not Bool or not Integer) to be able to use analog signals in future posts.

TNeuron = class(TObject)
private
FThreshold: Single;
FInputCount: Integer;
FInputs:TSingleDynArray;
FWeights:TSingleDynArray;
FAnalogOutput:Boolean;
function GetInput(Index: Integer): Single;
function GetOutput: Single;
function GetWeight(Index: Integer): Single;
procedure SetInput(Index: Integer; const Value: Single);
procedure SetWeight(Index: Integer; const Value: Single);
procedure SetInputCount(const Value: Integer);
public
Constructor Create(aInputCount:Integer);
procedure RandomInitialize;
property Threshold:Single read FThreshold write FThreshold;
property Inputs[Index: Integer]: Single read GetInput write SetInput;
property Weights[Index: Integer]: Single read GetWeight write SetWeight;
property InputCount:Integer read FInputCount write SetInputCount;
property AnalogOutput read FAnalogOutput write FAnalogOutput;
end;



The readonly Output property will be calculated each time it is called in GetOuput, so this function is the main execution of the neuron process in which it is decided if the neuron will fire or not. Normally the inputs will be provided from outside to the class, the source may be either the date to be recognized or other neuron signals. We will see the signal transmission in the next part of the subject. In this article we will focus on the single cell and its simple execution logic. The InputCount represents the receiving synapse count of the cell which is done by connecting the dendrites to other neurons’ axon terminals. So this count must be a variable which ve can arrange later according to our model. Each input is evaluated with its weight, so we will have a weight array which is equal to inputs array in size. In the constructor of the class, the dynamic arrays are created through the InputCount property setter and they are populated with initial random values.

constructor TNeuron.Create(aInputCount:Integer);
begin
inherited Create;
FAnalogOutput := False;
InputCount := aInputCount;
RandomInitialize;
end;

procedure TNeuron.SetInputCount(const Value: Integer);
begin
FInputCount := Value;
SetLength(FInputs,Value);
SetLength(FWeights,Value);
end;

procedure TNeuron.RandomInitialize;
var i:Integer;
begin
Randomize;
for i := 0 to FInputCount-1 do
begin
FInputs[i] := RandomRange(0,1);
FWeights[i] := Random*RandomFrom([-1,1]);
end;
FThreshold := Random;
end;


To be able to calculate the output value of the Neuron we will use the logic explained in above Figure 1. First we will calculate the cell load with the below summation method and after calculating the cell load, we will fire the signal if the load is greater than the inside threshold. Otherwise the result will be 0, which means no signal is fired.

$l=\sum_{i=1}^{n}weight_{i}*input_{i}$

function TNeuron.GetLoad: Single;
var i: Integer;
begin
Result := 0;
for i := 0 to FInputCount-1 do
begin
Result := Result+ FInputs[i]*FWeights[i];
end;
end;

function TNeuron.GetOutput: Single;
var i: Integer;
begin
Result := 0;
if Load > FThreshold  then Result := 1;
end;


Please note that, in this function GetOutput, the result may be either 0 or 1. Normally neurons make digital transfer between each other which we usually represent with 1 or 0. However, on network models, sometimes we will use an analog signal which represents the relation with the load of the cell and the threshold. This analog value which is mostly arranged as to be at the final output layer of the network may be used for two reasons. The first is to calculate the network error between the expected and the actual results. The second is , we sometimes really need analog values as output such as the velocity of an item.

So to be able to use analog outputs on demand, we should apply a lit bit more math on the load and threshold. You can fire the difference, the ratio or any relation between load and threshold. But the best one proved in many experimental practices is using the below exponential relation between them.

$s=\frac{1}{1+exp(-l+t)}$

If the AnalogOutput property of the Neuron is set to True, the output will be fired according to the above equation. So here is our new GetOutput function.

function TNeuron.GetOutput: Single;
var i: Integer;
begin
Result := 0;
if FAnalogOutput then