C# Random.Next(int, int) – how to define the bounds correctly

The devil is in the detail. Recently I wanted to create random numbers between 0 and 1 in C#. The Visual Studio tells us that we have to use Random.Next (int, int) method, whereby the first integer defines the lower limit and the second integer defines the upper limit. No more and no less. So based on Visual Studio’s description the following function should work:

Random rnd = new Random();
int random = rnd.Next(0,1);

Wrong! So you’ll get only one thing – in fact zeros. So I had to dip into the MSDN library to find a solution. There is explained, on the contrary to IntelliSense from Visual Studio, that the lower bound is inclusive and the upper bound is exclusive.


Type: System.Int32
The inclusive lower bound of the random number returned.

Type: System.Int32
The exclusive upper bound of the random number returned. maxValue must be greater than or equal to minValue.

In plain language this means, to […]

PHP: rand() vs. mt_rand() – what is more accurate, what is faster?

After I was pointed out, that it would be better to use the PHP function mt_rand() instead of rand() for generating random numbers (because mt_rand() should be more accurate), I decided to investigate on that statement. But at first, where is the difference between rand() and mt_rand()?
By use of mt_rand() you can generate random numbers just like with the rand() function. The function call also looks equal on both functions. Though the PHP documentation says mt_rand() is up to 4-times faster and would be create more random/arbitrary numbers. By implication this would mean, that the rand() function creates inaccurate random numbers.
I was not aware of this problem, but after some googleing I discovered that there are a lot of people who think that mt_rand() is the better function. However I had to notice that most of the contributions which complain that rand() is not clean are a “little older”.
Often cited […]

How to build a low cost Bluetooth relay

Bluetooth iconMeanwhile there are lots of solutions for home automation in the market. However the most of them share one flaw – their price. But blessedly there is Youtube where you can find a video for each topic…
On Youtube I came across a clever tinkerer named Lou, who shows how you can build a low cost Bluetooth relay. I think this is really cool, because there are a many application areas. You could open your garage door, switch lights on and off or unlock an electric lock. To build up one of these Bluetooth relays you must not even be a technical genius, because the use of a soldering iron is sufficient. Knowledge in programming or microcontroller-technology isn’t necessary.
For the Bluetooth relay you need only one transistor and a Bluetooth headset […]

How to get website header information in C#

With the following function you can easily retrieve a list of website headers in C#. The function needs only the url of a webpage to collect the data. If the function is successfully executed, you’ll get a Dictionary, typed as <string, string>, as return value. This Dictionary<string, string> contains all the headers of the webpage, whose url you passed by at function call time.

//Don’t forget to set the using-directive ;)
using System.Net;


public Dictionary getHeaders(string url)
//Dictionary which shall contain the header information
Dictionary header = new Dictionary();

//Create webrequest
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);

//Create response object
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

//Read all headers from the response
foreach (string headerItem in response.Headers)
//Add header to the dictionary

Download source code of a website in C#

With the help of the follwing short C# snippet, you’re able to receive the html code / source of any website as string. Such a function is useful, for example, if you want to parse some information from a website for later use.
Thus the snippet works, you have to add the following two using directives to the header of your source.

using System.Net;
using System.IO;

The function itself looks like this:

public string getHTML(string url)
//Create request for given url
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);

//Create response-object
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

//Take response stream
StreamReader sr = new StreamReader(response.GetResponseStream());

//Read response stream (html code)
string html = sr.ReadToEnd();

//Close streamreader and response

//return source
return html;

A function call could look like this:


If you’ve got suggestions or even problems with the snippet, just write a comment.