Keyvan Nayyeri

God breathing through me

Some Thoughts About Random Generation

Photo taken from http://casoilresource.lawr.ucdavis.edu/drupal/files/images/logistic-small.png Generating random values is a common scenario in programming and almost all programming languages have a built-in support for random value generation somehow.

The more common part of random value generation is generation of random numbers including integers and float point numbers so such feature is more common among programming languages.

But generating string values is another type of random value generation which is more common than the past in the modern programming scenarios. However, there are several ways to generate random integers and strings based on your development platform and your requirements.

In this post and a future post I'm going to talk about some topics related to random generation then talk about random string generation in .NET. I also will talk about pros and cons of each method to some extent.

For this post I just want to give some general notes about random generation in general that may be helpful for you.

Before talking about any technical details in the future post, let me give a short introduction and a background about this topic.

You may know that random generation is a topic in statistics in university degree. There are various random generation algorithms and methods with different features that are helpful for different requirements and we studied some of them at university (by the way, this is a sweet topic that I loved it so much). However, implementation of such methods isn't so easy and they've been a built-in part of development platforms from the early days.

I don't want to step into such details here but want to talk shortly about random generation and its aspects. Each random generation method is suitable for some requirements as I describe here. Here I'd say that random generation method is different from random generation algorithm in this post and simply refers to the techniques that we use to apply built-in API to generate a random value.

There are many cases when you need to generate a random integer or string to use in your applications. This may be an identifier for an entity, a registration key for a user, a support ticket ID for clients, a password or any other type of application that you can guess for a random value.

A random value can be generated in some ways but there are two types of random values in general:

  • Unique values
  • Repetitive values

There are some cases where you want a unique generated value while there may be some cases that you don't care about uniqueness of values so this is one of the first parameters that you need to consider about your random generation method.

On the other hand unique values can be grouped into two groups:

  • Unique values in a specific domain
  • Universally unique values

Obviously you can have unique values in a specific domain like your machine or your database table but they can be repeated in other domains like other machines or tables. A universally unique value is unique in all domains. A good example is GUID.

This is another parameter that you would consider when choosing your methods. For instance, if you're going to replicate your data from two machines then having a universal unique value is mandatory.

My emphasis is on this because I experienced its troubles a short while ago. A contractor at our military office had this fault in a database converter application and it caused lots of troubles for data replication! They asked me to follow up with him and other guys to get this problem solved so it made me nervous at the end!

The third parameter to consider is the length of the generated value. In some cases you need a specific length of values and other lengths may not be suitable. For example, you may want to have four digit numbers or strings with length of five.

The range of the value is another parameter especially for numeric values. For example, sometimes you want to generate a percent and number should be located in a specific range.

The fifth parameter, especially for string generation, is the type of characters or elements that you want to have in the randomly generated values. For instance, if you're going to use a random string in URLs then you wouldn't want some invalid characters or if you're going to generate a random password then you would need a limited set of characters.

These are some important aspects that you should consider when choosing a random generation method.

As random number generation is a well-documented and easy topic in .NET I don't talk about it at all. But I will discuss about random string generation methods in the next post. While I'm covering different methods, I will show you some pros and cons of each method to let you know which one is suitable for you.

4 Comments

Simone
Apr 02, 2008 1:58 PM
#

Good one Keyvan,

interesting to hear about "real" computer science from someone with a math background... I really love these topics but none talks about them in the dev communities... everybody is focused on architecture and design and software development that people forget that "real" programming is about computational math, algebra, matrix and that kind of stuff.


Keyvan Nayyeri
Apr 02, 2008 2:17 PM
#

@Simone:

Yes, such topics are covered more in mathematics and statistics and usually developers don't care about the origins of methods.

Btw, Phil and I should begin a blog about mathematical approaches in .NET development ;-)


Nyi Nyi Thann
Apr 20, 2008 8:46 PM
#

Hi Keyvan Nayyeri,

I like almost all ur posts.

*I should begin a blog about mathematical approaches in .NET development*

I couldn't wait.


My Best Blog Posts in 2008
Dec 31, 2008 1:40 PM
#

In the past 3.5 years of blogging, I haven’t had such best pick up collections in the end of the year, but now that everybody is writing one, why shouldn’t I write my own?! Collecting this list, I could realize some interesting facts that completely changed

Leave a Comment





Ads Powered by Lake Quincy Media Network