There are times when you want to generate a random number that is between a certain range, for example, between ‘x’ and ‘y’ where ‘y’ is larger than ‘x’. Many coding languages will have a random number generator and so creating a random number in theory using the native built in language function is not a problem. However, in many cases, this random number generator will create a random number between 0 and 1 and so in most cases, this is not a format which you will find useful because you would be looking for a random number between ‘x’ and ‘y’ and so you must convert this random number to fit in this range.
The way to go about doing this is actually rather simple.
Required Number = (Random Number * (MaxRange - MinRange) + MinRange
Posted on May 19th, 2007 by andai
Filed under: Software Development, Web Development
Leave a Reply