Saturday, September 26, 2009

2 ways to win at the Roulette

How to win at the Roulette?
A simple roulette bet yields 47% maximum winning chance. But if you have more rounds to play, you can achieve better odds. It all depends on your strategy. I designed an algorithm that yields 65% chance of winning some money after few rounds. To tell the whole history I wrote a whole open source C# .NET project that enables developers to write and test its own algorithms, for any kind of game of chance. All you need to is to checkout the project or download the zip and implement IAlgorithm. To see the results you should run the console application. If you agree with the fitness function would you write a better algorithm?

Tuesday, September 15, 2009

Thursday, July 2, 2009

Adding recombination to Image Vectorization Genetic Programming

After reading the Roger Alsing's arcticle about his Evo Lisa, a genetic approach to image vectorization, I decided to build my own version of it from scratch using C# .NET and TDD. The result is published at Google Code project hosting licensed under LGPL. The key diferences are:

 1. It uses only Triangles instead of polygons or splines.
 2. It accepts a population parameter.
 3. When the population > 1 then it does recombination (cross-over).

Tuesday, June 30, 2009

How to convert MP3 files to WAV files in .NET (C#)

I have successfully converted compressed audio to PCM files with the Alvas.Audio .NET library. Its FAQ shows how to convert a WAV to a MP3 but not the contrary. So I had to write my own code:

static void Main(string[] args) { string mp3File = @"Theme - Simpsons.mp3"; string wavFile = @"Theme - Simpsons.wav"; using (var wr = new Mp3Reader(File.OpenRead(mp3File))) { IntPtr mp3Format = wr.ReadFormat(); byte[] mp3Data = wr.ReadData(); var wavFormat = AudioCompressionManager.GetWaveFormat(mp3Format); Decode2Pcm(ref mp3Format, ref mp3Data, ref wavFormat); MemoryStream ms = new MemoryStream(); using (var output = File.OpenWrite(wavFile)) using (WaveWriter ww = new WaveWriter(output, AudioCompressionManager.FormatBytes(AudioCompressionManager.GetFormatList(wavFormat)[0].FormatHandle))) { ww.WriteData(mp3Data); } } } private static void Decode2Pcm(ref IntPtr format, ref byte[] data, ref WaveFormat wf) { IntPtr newFormat = AudioCompressionManager.GetCompatibleFormat(format, AudioCompressionManager.PcmFormatTag); byte[] buffer = AudioCompressionManager.Convert(format, newFormat, data, false); wf = AudioCompressionManager.GetWaveFormat(newFormat); format = newFormat; data = buffer; }

Thursday, June 11, 2009

Google AppEngine will reduce free quotas to 14%

Google published on its AppEngine blog the plan to reduce the free use quotas to 6.5 CPU-hours and 1.07 gigabytes of outbound transfer, by June 22nd. It's 14% of the current free CPU time quota and 10% of the current bandwith, and 8,000 of the currently free deployed applications will be charged.

Current free quotas:

46.30 CPU hours
10.00 GBytes Outgoing Bandwith
10.00 GBytes Incoming Bandwith
1.00 GBytes Stored Data
2000 Recipients Emailed

Saturday, June 6, 2009

How to draw on a Windows Form using C# .NET

1. Create a new Windows Forms Application on Visual Studio
2. In the design mode click on the form
3. On the properties window click on events
4. Double click the "Paint" event

and the final step:

5. Put the code below in the file that opens:

using System;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApplication1
    public partial class Form1 : Form
        private Bitmap DrawingArea;  // Area to draw on.

        public Form1()

        private void Form1_Load(object sender, EventArgs e)
            DrawingArea = new Bitmap(
            using (var canvas = Graphics.FromImage(DrawingArea))
                canvas.FillPolygon(Brushes.Black, new PointF[] { new PointF(10, 10), new PointF(0, 0), new PointF(0, 10) });

        private void Form1_Paint(object sender, PaintEventArgs e)
            e.Graphics.DrawImage(DrawingArea, 0, 0, DrawingArea.Width, DrawingArea.Height);

Wednesday, June 3, 2009

How to convert a Brush to Color in C#

Or How to get the Color of a Brush object in .NET

The solution is pretty simple:
 new Pen(brush).Color

The reverse is also possible

 new SolidBrush(color)

Monday, June 1, 2009

Object Oriented Javascript, C# Comparison

C# Code:

public class ClassName : BaseClassName
public object PropertyName { get; set; }

public BaseClassName(object argumentName)
this.PropertyName = argumentName;

public void MethodName()

Javascript Code:

function ClassName(argumentName) {
this.PropertyName = argumentName;

ClassName.prototype.MethodName = function() {

Sunday, March 1, 2009

Test your audio spectral sensivity

I have created a sound file, which plays all musical notes hearable by the humans. It is the pure tone audiometry file, with that file you can test your hearing capabilities because it has all notes played with the same amplitude, but you will hear them at different levels and some of them will not be heard at all.

Download and play the file (900 KB):

In the following spreadsheet there is each sound described:
list of musical notes

Below is the Matlab code to generate the file above

fs = 44100;
t = 1 : 1/fs : 1.5;
exponents = (-5 + 6/12 ): 1/12 : (5 + 7/12);
frequencies = 440 * 2 .^ exponents;
allNotes = sin(2 * pi * (transp(t) * frequencies));
whole = [];
for index=1:size(allNotes,2)
whole = [ whole transp(allNotes(:,index))];
wavwrite(whole, fs, 8, 'audiometry.wav')

Sunday, February 8, 2009

New open-source project: EulerMath

Project Euler has many mathematical challenges, which can be solved through computer programs. The .NET's Math class doesn't provides all mathematical functions needed for the solution of those problems.

Because of this I created EulerMath. The main goal of this project is to create a fast and robust library that provides all common mathematical functions that are necessary to those problems's solutions.

Monday, February 2, 2009

The best developers forum is a well known developers forum. There you can ask question about any language and the answer comes in less than a minute for the mainstream languages, and in less than an hour for languages like Matlab.

The main feature that makes that site better than any forum is a clean (KISS) and useful interface, not resembling any other forum.

Please visit my profile there, where you can see what I've been asking, answering and commenting:

Saturday, January 24, 2009

Quote of the day: How will the world's doom come?

The most likely way for the world to be destroyed, most experts agree, is by accident. That's where we come in; we're computer professionals. We cause accidents.
- Nathaniel Borenstein