Get a Float From RandomNumberGenerator

Standard Random class works perfectly fine most of time. However, on bigger sample, you will see some non-random tendencies. Much better random source is RandomNumberGenerator.

Unfortunately RandomNumberGenerator does not actually return random numbers. It returns random bytes. It is our duty to change those random bytes to single double value ranging from 0 to 1 (as from NextDouble function).

Idea is to get 4 random bytes and to convert them to unsigned integer (negative numbers are so passé). If that number was to be in 0 to 1 range it would be enough to divide it by UInt32.MaxValue. Since we need result to be less than 1, we have slightly larger divisor:

private RandomNumberGenerator Rnd;

private double GetRandom() {
    if (this.Rnd == null) { this.Rnd = RandomNumberGenerator.Create(); }
    var bytes = new byte[4];
    this.Rnd.GetBytes(bytes);
    var number = BitConverter.ToUInt32(bytes, 0);
    return number / (UInt32.MaxValue + 1.0);
}

* Drive

It seems to me that Internet drives are everywhere.

For a while we had DropBox as undisputed king of remotely synchronized files. When it prove successful, we got bunch of others, among which SugarSync seemed like most serious contender.

This week things got interesting with Microsoft introducing SkyDrive. And today we got Google onboard with Google Drive.

Only company missing action is Amazon. Yes, they have their drive also but they are missing half-decent sync client.

With so many heavyweight players around, things are bound to get interesting soon. :)

Not Too Unique

My personal favorite when it comes to databases is Microsoft SQL Server. However, reality dictates that I sometime have to use different database. Actually quite often I need to have one application supporting different database servers. I might need application to work on SQL Server, PostgreSQL and (god forbid) Oracle.

With time I got used to prefer standardized parts of SQL. It makes life a lot simpler and SQL Server is usually ok with that plan. Except when it comes to NULLs where things can get little bit quirky. Setting ANSI_NULLS to true does sort most of issues but one.

If you have unique index on nullable column, SQL compliant behavior is to force uniqueness on all values except null ones. That means that having "a", "b", null, null is valid scenario. In SQL Server that second null will not be allowed under premise that such value already exists and thus violates unique index. That “feature” is part of SQL Server since it’s very first days and it is very unlikely that it will be changed in the future. Compatibility is a bitch sometime.

Fortunately there is a workaround since SQL Server 2008 in form of filtered indexes. That enables unique index on only some values. In our scenario we just need to ignore nulls and standard behavior here we come:

CREATE UNIQUE INDEX UX_MyIndex ON MyTable(MyColumn) WHERE MyColumn IS NOT NULL;

Nice, With Crap

Illustration

There is a trend to piggyback totally unrelated applications together in setup. Example that I faced recently was GOM Player bundled with AVG “security toolbar”.

I am surprised with two things here. AVG was quite reputable company for some time and they had quite a good free antivirus client. They do not seem like company that needs this kind of “distribution”.

Second thing is description that reads “set, keep and protect AVG secure search as my default …”. Frankly, that sounds like description of an toolbar that get installed when you visit naughty pages. Those toolbars also set, keep and protect their defaults. It is quite a fall from grace when security software and malware have exactly same description.

And, of course, standard installation includes all malware by default. You must actively make total of three clicks to remove it. It might not seem much but that also means that I cannot just Next/Next/Finish through installation. And that means that I cannot recommend this application to any inexperienced user. Explaining them how to remove crap would just take too much energy and I can bet that half of them would install everything.

Do not misunderstand me, I am fine (or even in favor) with piggybacking as long as other program has anything to do with program I am installing. Great example would be Wireshark including WinPcap. That is match that works. And that is software I trust.

When software goes on junkware route, there is only one thing to do. I will stop using GOM Player, like I stopped using BSPlayer before it. Irony is that I liked both of them and I am leaving them for same reason. Anybody has any decent (and junk-free) multimedia player to recommend?

PBKDF2 With SHA-256 (And Others)

.NET has built-in class handling all your PBKDF2 needs. It is called Rfc2898DeriveBytes and it works as long as you stick to SHA-1 HMAC. If your needs move in direction of SHA-256, you are out of luck.

Therefore I created Pbkdf2 class that takes any HMAC algorithm (e.g. SHA-1, SHA-256 or SHA-512) as input and allows you to derive key based on it.

using (var hmac = new HMACSHA256()) {
    var df = new Pbkdf2(hmac, password, salt, iterations);
    Console.WriteLine(BitConverter.ToString(df.GetBytes(32)));
}

Full code is available for download.