Nov 21 2016

How to generate Random numbers

Category: Algorithm | Maths ashish sheth @ 14:58

If you want to generate a random number for some business logic you are implementing, what would you do?

You would use Random class if you use Java or C#. Most programming language has some library function or class to give you random number.

But suppose you need to produce random numbers by your own without using any library function what would you do?

There are many algorithms to use to produce random number and here I will demonstrate a very basic algorithm which uses the modulo operator (%) in C#. The goal is not to come up with a foolproof algorithm to generate random numbers, the goal is to just use simple math trick to understand how random numbers can be generated. If you really need to generate random numbers in your programs then you should use the inbuilt library functions provided by the language or framework you are using.

You know what is modulo (%) operator is, right? It gives you the remainder when you divide the left hand number by right hand number.

so doing 20 % 20 will give you 0. And 20 % 19 will give you 1 and 20 % 18 will give to 2 and so on.

20 % 20 = 0
20 % 19 = 1
20 % 18 = 2
20 % 17 = 3
20 % 16 = 4
20 % 15 = 5
20 % 14 = 6
20 % 13 = 7
20 % 12 = 8
20 % 11 = 9
20 % 10 = 0
20 % 9 = 2
20 % 8 = 4
20 % 7 = 6
20 % 6 = 2
20 % 5 = 0
20 % 4 = 0
20 % 3 = 2
20 % 2 = 0
20 % 1 = 0

You can see when you divide 20 by numbers from 1 to 20 you get number 0 to 9 as remainders. The trick is the larger the dividend, larger the range of number you get as remainders.

So let’s set dividend d to a some large number, for the purpose of this post I will choose 10000. This will give you the range of 0 to 4999 as remainders.

But as you can see, this method produces sequential numbers, not random numbers. Well, on every iteration you use the remainder to produce a new dividend and you can see that instead of sequential numbers you are getting the random numbers.

So, let’s use below equation to produce a new dividend on every iteration, which uses the current remainder as new dividend:

remainder = a * current remainder + b % divisor

Using the above equation, we get below result, when a = 100, b = 100 and divisor is set to 19:

2100 % 19 = 10
1100 % 19 = 17
1800 % 19 = 14
1500 % 19 = 18
1900 % 19 = 0
100 % 19 = 5
600 % 19 = 11
1200 % 19 = 3
400 % 19 = 1
200 % 19 = 10
1100 % 19 = 17
1800 % 19 = 14
1500 % 19 = 18
1900 % 19 = 0
100 % 19 = 5
600 % 19 = 11
1200 % 19 = 3
400 % 19 = 1
200 % 19 = 10

Notice that the above equation produces a random number on every step but it repeats after few iteration. This is because the chosen values of the a, b and divisor.

Setting the divisor to a really large value can give us random numbers which may not repeat to soon. Such as below:

100 % 12345 = 100
10100 % 12345 = 10100
1010100 % 12345 = 10155
1015600 % 12345 = 3310
331100 % 12345 = 10130
1013100 % 12345 = 810
81100 % 12345 = 7030
703100 % 12345 = 11780
1178100 % 12345 = 5325
532600 % 12345 = 1765
176600 % 12345 = 3770
377100 % 12345 = 6750
675100 % 12345 = 8470
847100 % 12345 = 7640
764100 % 12345 = 11055

1105600 % 12345 = 6895 

689600 % 12345 = 10625
1062600 % 12345 = 930
93100 % 12345 = 6685
668600 % 12345 = 1970

Here, a and b is set to 100 and divisor is set to 12345, while current remainder is initialized to 0 at the start. Note that setting divisor to 12345 did not produce repeated numbers in the first 20 iterations, but it can still produce repeated numbers after few hundred iterations. But you get the idea, right?

The equation a + b * currentRemainder  % divisor is called Linear Congruential Generator. You can read more about it here.

Tags: ,

Oct 20 2014

Manage your emails faster with Outlook Quick Steps

Category: Productivity ashish sheth @ 22:34

Just noticed this setion on my Outlook toolbar:

image

When I saw the above in my outlook toolbar I realized that it could help in a lot of productivity gain while managing email.

There are pre-built steps which you can use to manage email as mentioned in the above image. You can add your own quick steps too. Notice that “Archive” on the top-left. I setup that by myself as follows:

Click on the “down arrow” on the left-bottom part (highlighted in blue) of the above screen.  A “Manage Quick Steps” dialog box will be opened as below:

image

Clicking on the “New” button will open up a dialog box as below, where you can setup the action that you want to take. Note that you can also setup a shortcut key which you can use for the action instead of every time selecting the action from the menu.

image

Tags: , ,

Sep 30 2014

A Quote about Best Practices

Category: Quotes ashish sheth @ 20:28

Obeying best practices blindly is not a best practice.

From this answer on stackoverflow

Tags:

Sep 18 2014

Tackling the Arrow head Anti-Pattern

Category: .Net | C# | Design Principles ashish sheth @ 00:13

Frequently you will see following type of code in your code base.

if(result != 1)
{
    if(someOtherResult == 101)
    {
        if(anotherValue == 500)
        {
            // do something
        }
    }
    else
    {
        // do some other thing
    }
}

return;

Here the code forms a shape of an arrow-head, as below:

if
    if
        if
            if
                do something
            end
        end
    end
end

If you see, the main logic is deep down into the nested condition, it increases the cyclomatic complexity of the code.

A better version of the same code could be as below:

if(result == 1)
{
    return;
}

if(someOtherResult == 101 && anotherValue == 500)
{
    // do something
    return;
}

// do some other thing

return;

The above code does a number of things to flatten the code and make it better:

  1. Validations are performed first and it returns at the first opportunity.
  2. Multiple nested conditions are combine into one (with “&&”(logical And) operator. If there are multiple expressions forming one such condition, they can be moved to a separate method returning boolean. That method can then be use in the if condition as below:
if(IsValidResult(someOtherResult, anotherResult)
{
    // do something
    return;
}

bool IsValidResult(int someOtherResult, int anotherResult)
{
    if(someOtherResult == 101 && anotherValue == 500)
    {
        return true;
    }
    return false;
}

Tags: , ,

Sep 16 2014

Podcast that I listen

Category: .Net | Learning | miscellaneous ashish sheth @ 00:45

In the big sea of web, you will find many people telling you the podcast that every developers should listen. Here is my list, and yes, I too think these are the podcast every developer(.Net developer) should listen.

  1. .Net Rocks: This is more than a decade old talk show focusing on Microsoft .Net Technologies. Earlier it used to be a weekly show, but now it has three show a week. Apart from covering anything and everything related to .Net, it also covers mobile app development on Android and iOS.
  2. Hanselminutes: This is the show by Scott Hanselman, which focuses on technology including Microsoft .Net Technology.
  3. HerdingCode: This is a show where discussion goes into nitty-gritty of development tasks. As with the above two, not just the .Net technology, but other aspects of Software development practises are also discussed.
  4. Software Engineering Radio: This is a monthly podcast targeted towards not just .Net developers, but to all software developers in general. So it covers topics which include Software Engineering practises, architecture, design patterns, etc. It also has a rather academic feel and many a times the guests include leading researchers in the field of Computer Science.
  5. MS Dev show: This is relatively a new podcast for Microsoft developers focussing fully to Microsoft technology including topics such as Azure/Cloud, Windows, Windows Phone, .Net etc.
  6. IEEE Software’s On Computing: In this podcast Grady Booch reads from his column “On Computing” in the IEEE Software magazine. In this, he talks software’s impact on humanity.
  7. IEEE Software’s Tools of the Trade: This is again the podcast of an on going column in IEEE Software with the same name. In every episode, it covers software tools to be used within a certain aspect of Software Development, such as documentation, debugging, testing, etc.
  8. HBR IdeaCast: This is a weekly podcast from HBR.org and it is mostly about analysis and advice from leading minds in management.

Let me know what you listen to.

Tags: ,

Oct 17 2013

Domain Specific Languages - MindMap

Category: ashish sheth @ 06:07

Here is the MindMap that I prepared for Domain Specific Languages after listening to the podcast on the same topic by Martin Fowler on SE Radio.

 

Download Larger size here: Domain Specific Languages.png (164.27 kb)

Tags: ,

Jun 26 2013

History of Computing

Category: miscellaneous ashish sheth @ 23:34

An interesting infographic on history of computing.

Tags:

Jun 17 2011

.Net Guidance Map

Category: .Net | .Net Framework | asp.net ashish sheth @ 18:05

 

I thought these would be very helpful to those who are learning .Net.  J. D. Meier has put up Development Guidance Map for various .Net technologies. This material includes links to technical articles, videos, how-tos, blogs, tutorials, trainings, code samples on various .Net technologies and about everything you might need to know about .net.

Happy Learning.

 

Tags: ,

Sep 16 2010

Use Properties to Encapsulate the Hidden Fields or ViewState in Asp.Net

Category: .Net | asp.net | C# | VS2008 ashish sheth @ 05:16

In asp.net you might be using lot of hidden variables and ViewState to maintain state between page postbacks. This can make your code look cluttered. You can use properties to encapsulate the hidden fields or ViewState.

If you are writing lot of code like this:

if(myHidden.Value == string.Empty)
{
	myHidden.Value = "someValue"; 	
}

someVariable = myHidden.Value

You can use properties to encapsulate the access to myHidden field. For example:

public string MyHiddenFieldValue
{
	get
	{
		if(myHidden.Value == string.Empty)
		{
			myHidden.Value = "someValue"; 	
		}
		return myHidden.Value
	}
	set
	{
		myHidden.Value = value;
	}
}

Then you can access the myHidden field just by the property

someVariable = MyHiddenFieldValue;

Similarly if you are storing some custom values in the ViewState of the page the you can create property for the ViewState also.

public string MyCustomViewState
{
	get
	{
		if(ViewState["MyViewState"] == null)
		{
			ViewState["MyViewState"] = "someValue"; 	
		}
		return ViewState["MyViewState"].ToString();
	}
	set
	{
		ViewState["MyViewState"] = value;
	}
}

Tags: , , ,

Aug 24 2010

VisualStudio ReflectedSchemas folder in ApplicationData

Category: .Net | asp.net | VS2008 ashish sheth @ 23:23

Just now I found out that C:\Documents and Settings\[Username]\Application Data\Microsoft\VisualStudio\9.0\ReflectedSchemas

folder takes lots of space. VS 2008 stores schema files which are dynamically generated when you compile a webcontrol.

To release the space on your drive you can delete the files safely and VisualStudio will re-generate these files whenever needed.

 

For more information see below:

http://blogs.msdn.com/b/mikhailarkhipov/archive/2004/05/14/131949.aspx

http://stackoverflow.com/questions/878345/reflectedschemas-folder-in-the-users-appdata-folder-visual-studio

http://forums.asp.net/t/1264942.aspx

http://www.ureader.com/msg/15364998.aspx

Tags: , ,