ASP Kitchen: ASP.NET Articles:
Generating Random Numbers in the .NET Framework using C#
Generating Random Numbers in the .NET Framework using C#Rather see the code samples in VB.NET? Read the article Generating Random Numbers in the .NET Framework using VB.NET instead! Random numbers may be generated in the .NET Framework by making use of the Random class. This class may be instantiated using the following code: //Create a new Random class in C# Random IntegersOnce the class has been instantiated, a random integer can be obtained by calling the Next method of the Random class: //C# The value of RandomNumber will, therefore, be assigned a random whole number between 1 and 2,147,483,647. In most coding situations, it is more desirable to create a random number within a certain size range. In this case, the Next method should be called with two arguments: the minimum value and the maximum value. For example, the following assigns RandomNumber to a value that is greater or equal to 4 and less than 14: //C# Note that an ArgumentOutOfRangeException will be raised if the minimum value is larger than the maximum value. It is also possible to specify just the maximum value using a different constructor. The following will return a return a random integer that is greater or equal to 0 and less than 14: //C# Again, an ArgumentOutOfRangeException will be raised if the maximum value is smaller than 0. Random Floating Point NumbersAs well as returning random integers, the Random class can also return floating point numbers. The NextDouble method returns a random number as a Double. The random number's value is always greater or equal to 0.0, and less than 1.0: //Create a random double using C# Useful Development Tools
Author detailsBrett Burridge has worked as a web developer since 1997 and has developed web applications for a range of corporations, start up busiensses and educational establishments. Brett is presently employed as an Internet developer and technical writer through his own company, Winnersh Triangle Web Solutions Limited. The company produces a number of innovative products, including a range of software documentation tools, which include the ASP Documentation Tool, the .NET Documentation Tool for VB.NET and C#, and the SQL Server Documentation Tool. Other products include The Website Utility, which functions as a website error checker, search engine optimizer and ASP/ASP.NET search engine builder application. As well as the ASPAlliance, Brett has written articles for Ariadne.ac.uk, ASPToday, the software documentation portal www.softwaredocumentation.info, and has contributed recipes to the ASP.NET Developer's Cookbook. links Outside web development, Brett is interested in travelling (here are my travel logs from New York, Hong Kong and Tokyo), digital photography (here's my photo gallery), tropical fishkeeping and collecting contemporary works of art by artists such as Doug Hyde. Contact Brett by emailing Article history"Generating Random Numbers in the .NET Framework using C#" published on ASPAlliance.com on 14 November 2004. ASP Kitchen: ASP.NET Articles: Generating Random Numbers in the .NET Framework using C# |
|
||||||||||||||||||||||||
| © page content copyright Brett Burridge 1998 - 2008. | |||||||||||||||||||||||||