Posts

Showing posts from November, 2016

How to generate Random numbers

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. ...