Source Code for http://www.brettb.com/RandomNumbersInDotNet.asp


<html>

<head>
<title>Generating Random Numbers in the .NET Framework</title>
<link REL="stylesheet" HREF="BrettbDotCom.css" TYPE="text/css">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<meta name="description"
content="Describes how to generate random integer and floating point numbers using Microsoft's .NET Framework. Code samples are supplied in both VB.NET and C#">
<meta name="keywords"
content="ASP.NET, random, next, nextdouble, generator, generated, CSharp, C#, ASP.NET, VB, Visual Basic, Visual Basic.NET">
</head>

<body>
<script language="JavaScript">

function SubmitHeaderSearchForm() {

document.FormSubmitHeaderSearch.submit();

}

</script>
<div align="center"><table border="0" cellpadding="0" cellspacing="0" width="800"
style="border: 1px solid rgb(0,0,0)">
<tr>
<td width="167"><a href="Default.asp" title="Brettb.Com (Back to Home Page)"><img src="Images/BrettbDotCom_Logo.jpg" width="167" height="72"
alt="Brettb.Com" border="0"></a></td>
<td width="633"><img src="HeaderImages/PurpleFlame.jpg" width="633" height="72" alt=""></td>
</tr>
</table><table border="0" cellpadding="0" width="802" bgcolor="#DD3333"
style="border-left: 1px solid rgb(0,0,0); border-right: 1px solid rgb(0,0,0); border-top: 1px none rgb(0,0,0); border-bottom: 1px solid rgb(0,0,0)">
<tr>
<td class="TDHeader" valign="middle" align="left">  <a href="Default.asp"
title="HOME">HOME</a> | <a href="web.asp" title="ABOUT ME">ABOUT ME</a> | <a
href="Biotechnology.asp" title="BIOTECHNOLOGY">BIOTECHNOLOGY</a> | <a
href="technicalwriting.asp" title="ARTICLES">ARTICLES</a> | <a
href="DeveloperTools.asp" title="DEVELOPER TOOLS">TOOLS</a> | <a href="Gallery.asp"
title="GALLERY">GALLERY</a> | <a href="contact.asp" title="CONTACT">CONTACT</a></td>
<form method="POST" action="SearchResults.asp" name="FormSubmitHeaderSearch"><td align="right" class="TDHeader" valign="middle">Search: <input type="text" name="query"
size="20" maxlength="100"> <a href="javascript:SubmitHeaderSearchForm();">Go</a></td></form>
</tr>
</table>
</center></div>
<div align="center"><center>

<table border="0" cellpadding="8" cellspacing="0" width="802"
style="border-left: 1px solid rgb(0,0,0); border-right: 1px solid rgb(0,0,0)">
<tr>
<td bgcolor="#E0E0E0" width="151" valign="top" align="left" class="TDLeftPanel" nowrap><strong><a href="DeveloperTools.asp" title="DEVELOPER TOOLS">DEVELOPER TOOLS</a><br>
</strong> <a href="ASPDocumentationTool.asp" title="ASP Documentation Tool">ASP Doc
Tool</a><br>
 <a href="ASPNetDocumentationTool.asp" title="ASP.NET Documentation Tool">ASP.NET Doc
Tool</a><br>
 <a href="http://www.winnershtriangle.com/w/Products.SQLDocumentationTool.asp" title="SQL Documentation Tool" target="_blank">SQL Doc
Tool</a><br>
 <a href="IndexServerCompanion.asp" title="Index Server Companion">Index Server
Companion</a><br>
 <a href="TheWebsiteUtility.asp" title="The Website Utility">The Website Utility</a><p><strong>TECHNICAL
ARTICLES <br>
</strong> <a href="ASPWatchArticles.asp" title="ASP Articles">ASP</a><br>
 <a href="ASP.NETArticles.asp" title="ASP.NET Articles">ASP.NET</a><br>
 <a href="JavaScriptArticles.asp" title="JavaScript Articles">JavaScript</a><br>
 <a href="SQL_Help.asp" title="Transact SQL Articles">Transact SQL</a></p>
<p><strong>PHOTO GALLERIES</strong><br>
 <a href="CanonEOS300D_Gallery1.asp" title="Canon EOS 300D Samples">Canon EOS 300D
Samples</a><br>
 <a href="Red_Arrows_2004.asp" title="Red Arrows 2004">Red Arrows 2004</a><br>
 <a href="Living_Coasts_Photos.asp" title="Living Coasts">Living Coasts</a><br>
 <a href="backgrounds.asp" title="Web Page Backgrounds">Web Page Backgrounds</a><br>
 <a href="gallery.asp" title="More Galleries...">More Galleries...</a></p>
<p><strong>NEW STUFF</strong><br>
 <a href="TransactSQLColorCoder.asp" title="Transact SQL Color Coder">SQL Color Coder</a><br>
 <a href="CanonEOS300D_Gallery3.asp" title="Canon EOS 300D Samples">Canon EOS 300D
Samples</a><br>
 <a href="TheWebsiteUtility.asp" title="The Website Utility">The Website Utility</a><br>
 <a href="Website_Search_Engine_Optimisation.asp" title="Useful Search Engine Optimization Techniques">Search Engine Optimisation</a><br>
 <a href="BuildingAnASPSearchEngine.asp" title="Creating an ASP Search Engine for your Website">Build an ASP Search Engine</a><br>
 <a href="MyTropicalFishtank.asp" title="My Tropical Freshwater Fishtank">My Tropical Fishtank</a><br>
 <a href="Investments_ISAs.asp" title="Savings & Investments">Savings & Investments</a><br>
 <a href="what's_new.asp" title="What's New?">Other New Stuff...</a></p>
<p><strong>POPULAR STUFF</strong><br>
 <a href="VBScriptRegularExpressions.asp" title="VBScript Regular Expressions">Regular
Expressions</a><br>
 <a href="ASPDocumentationTool.asp">ASP Documentation Tool</a><br>
 <a href="SearchingIndexServerWithASP.asp" title="Index Server & ASP">Index
Server & ASP</a><br>
 <a href="js_banner_ad_rotator.asp" title="JavaScript Banner Ad Rotator">JavaScript Ad Rotator</a></p>
<p><strong>LINKS</strong><br>
 <a href="http://www.winnershtriangle.com/w/Default.asp" title="Business Website"
target="_blank">Business Website</a><br>
 <a href="http://authors.aspalliance.com/brettb/" title="ASPAlliance Articles"
target="_blank">ASPAlliance Articles</a><br>
</p>
<p><img alt="Microsoft Certified Professional"
src="images/MCP_c_smaller.gif" width="151" height="38"></p>

<p align="center">
<script type="text/javascript"><!--
google_ad_client = "pub-7044749527879330";
google_alternate_color = "FFFFFF";
google_ad_width = 120;
google_ad_height = 90;
google_color_border = "000000";
google_color_bg = "DDDDDD";
google_color_link = "FF0000";
google_color_url = "DD3333";
google_color_text = "CCCCCC";
google_ad_format = "120x90_0ads_al_s";
google_ad_channel ="6714185213";
//--></script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</p>

<p align="center">
<SCRIPT type='text/javascript' language='JavaScript' src='http://xslt.alexa.com/site_stats/js/s/a?amzn_id=brettbcom&url=www.brettb.com'></SCRIPT>
</p>
</td>
<td width="551" valign="top" align="left">


<p><a href="Default.asp" title="Home">Home</a> > <a href="ASP.NETArticles.asp"
title="ASP.NET Articles">ASP.NET Articles</a></p>

<h1>Generating Random Numbers in the .NET Framework</h1>

<p>Random numbers may be generated in the .NET Framework by making use of the <b>Random</b>
class. This class may be instantiated using the following code:</p>

<p><font color="#008000">//Create a new Random class in C#</font><br>
<font color="#000080">Random RandomClass = new Random();</font></p>

<p><font color="#008000">'Create a new Random class in VB.NET</font><br>
<font color="#000080">Dim RandomClass As New Random()</font></p>

<h2>Random Integers</h2>

<p>Once the class has been instantiated, a random integer can be obtained by calling the <b>Next</b>
method of the Random class:</p>

<p><font color="#008000">//C#<br>
</font><font color="#000080">int RandomNumber = RandomClass.Next();</font></p>

<p><font color="#008000">'VB.NET</font><br>
<font color="#000080">Dim RandomNumber As Integer<br>
RandomNumber = RandomClass.Next()</font></p>

<p>The value of <i>RandomNumber</i> will, therefore, be assigned a random whole number
between 1 and 2,147,483,647.</p>

<p>In most coding situations, it is more desirable to create a random number within a
certain size range. In this case, the <b>Next</b> method should be called with two
arguments: the minimum value and the maximum value. For example, the following assigns <i>RandomNumber</i>
to a value that is greater or equal to 4 and less than 14:</p>

<p><font color="#008000">//C#</font><br>
<font color="#000080">int RandomNumber = RandomClass.Next(4, 14);</font></p>

<p><font color="#008000">'VB.NET</font><br>
<font color="#000080">Dim RandomNumber As Integer<br>
RandomNumber = RandomClass.Next(4, 14)</font></p>

<p>Note that an <em>ArgumentOutOfRangeException</em> will be raised if the minimum value
is larger than the maximum value.</p>

<p>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:</p>

<p><font color="#008000">//C#<br>
</font><font color="#000080">int RandomNumber = RandomClass.Next(14);</font></p>

<p><font color="#008000">'VB.NET</font><br>
<font color="#000080">Dim RandomNumber As Integer<br>
RandomNumber = RandomClass.Next(14)</font></p>

<p>Again, an <em>ArgumentOutOfRangeException</em> will be raised if the maximum value is
smaller than 0.</p>

<h2>Random Floating Point Numbers</h2>

<p>As well as returning random integers, the Random class can also return floating point
numbers. The <b>NextDouble</b> method returns a random number as a <i>Double<i>.</i></i>
The random number's value is always greater or equal to 0.0, and less than 1.0:<i><i></p>
</i></i>

<p><font color="#008000">//Create a random double using C#</font><br>
<font color="#000080">double RandomNumber = RandomClass.NextDouble();</font></p>

<p><font color="#008000">'Create a random double using VB.NET</font><br>
<font color="#000080">Dim RandomNumber As Double
<br>RandomNumber = RandomClass.NextDouble()</font></p>


<h2>Useful Development Tools</h2>
<div align="left">

<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td width="100%" valign="top"><a
href="http://www.winnershtriangle.com/w/Products.ASPDocumentationTool.asp" target="_blank" title="ASP Documentation Tool™"><strong>ASP
Documentation Tool™</strong></a></td>
</tr>
<tr>
<td width="100%" valign="top">Automatically creates developer documentation for ASP 2.0
and 3.0 web applications written in VBScript and JScript. Documentation for Microsoft
Access, SQL Server 7/2000 databases and Visual Basic 6.0 components associated with the
web application can also be incorporated into the reports. Documentation is created in
HTML, HTML Help and plain text formats.</td>
</tr>
<tr>
<td width="100%" valign="top">
   <a
href="http://www.winnershtriangle.com/w/SampleWeb_Documentation/SampleWeb.chm"><img
src="images/Icon_CHM.gif" width="16" height="16" alt="View Sample Output (HTML Help format)"
border="0" align="absmiddle"></a> <a
href="http://www.winnershtriangle.com/w/SampleWeb_Documentation/SampleWeb.chm" title="View Sample Output (HTML Help format)">View Sample
Output</a> (HTML Help format).<br>
   <a
href="http://www.winnershtriangle.com/w/SampleWeb_Documentation/Default.htm"><img
src="images/Icon_HTML.gif" width="16" height="16" alt="View Sample Output (HTML Format)"
align="absmiddle" border="0"></a> <a
href="http://www.winnershtriangle.com/w/SampleWeb_Documentation/Default.htm"
target="_blank" title="View Sample Output (HTML Format)">View Sample Output</a> (HTML Format).<br>
   <a
href="http://www.brettb.com/redirector.asp?URL=downloads/ASPDocumentationTool_Evaluation_Version.zip"><img
src="images/Icon_Download.gif" width="18" height="20" alt="Download Trial Version"
border="0" align="absmiddle"></a> <a
href="http://www.brettb.com/redirector.asp?URL=downloads/ASPDocumentationTool_Evaluation_Version.zip" title="Download Trial Version">Download
Trial Version</a> (5.2Mb ZIP file).</td>
</tr>
</table>
<br>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td width="100%" valign="top"><a
href="http://www.winnershtriangle.com/w/Products.ASPNETDocumentationTool.asp" target="_blank" title="ASP.NET Documentation Tool"><strong>ASP.NET Documentation Tool</strong></a></td>
</tr>
<tr>
<td width="100%" valign="top">Automatically creates developer documentation for ASP.NET web applications written in C# or VB.NET.
Documentation for SQL Server 7/2000 databases and C#/VB.NET components associated with the
web application can also be incorporated into the reports. Documentation is created in
HTML, HTML Help and plain text formats.</td>
</tr>
<tr>
<td width="100%" valign="top">
   <a href="http://www.winnershtriangle.com/w/ASP.NETDocToolSamples/SampleWeb.NET/SampleWeb.NET.chm"><img
src="images/Icon_CHM.gif" width="16" height="16" alt="View Sample Output (HTML Help format)"
border="0" align="absmiddle"></a> <a
href="http://www.winnershtriangle.com/w/ASP.NETDocToolSamples/SampleWeb.NET/SampleWeb.NET.chm" title="View Sample Output (HTML Help format)">View Sample
Output</a> (HTML Help format).<br>
   <a
href="http://www.winnershtriangle.com/w/ASP.NETDocToolSamples/SampleWeb.NET/Default.htm"><img
src="images/Icon_HTML.gif" width="16" height="16" alt="View Sample Output (HTML Format)"
align="absmiddle" border="0"></a> <a
href="http://www.winnershtriangle.com/w/ASP.NETDocToolSamples/SampleWeb.NET/Default.htm"
target="_blank" title="View Sample Output (HTML Format)">View Sample Output</a> (HTML Format).<br>
   <a
href="http://www.brettb.com/redirector.asp?URL=downloads/ASP.NETDocumentationTool_1.0.zip"><img
src="images/Icon_Download.gif" width="18" height="20" alt="Download Trial Version"
border="0" align="absmiddle"></a> <a
href="http://www.brettb.com/redirector.asp?URL=downloads/ASP.NETDocumentationTool_1.0.zip" title="Download Trial Version">Download
Trial Version</a> (2.9Mb ZIP file).</td>
</tr>
</table>
<br>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td width="100%" valign="top"><a
href="http://www.winnershtriangle.com/w/Products.SQLDocumentationTool.asp" target="_blank" title="SQL Documentation Tool"><strong>SQL
Documentation Tool</strong></a></td>
</tr>
<tr>
<td width="100%" valign="top">The SQL Documentation Tool creates technical documentation for Microsoft SQL Server 7.0 and 2000 databases. Technical documentation is created in HTML and HTML Help formats. The HTML Help format documentation is fully searchable and cross referenced. The SQL Documentation Tool documents SQL Server Tables, Views, Stored Procedures, Triggers and Table Relationships.</td>
</tr>
<tr>
<td width="100%" valign="top">
   <a
href="http://www.winnershtriangle.com/w/SQLDocumentationTool/Reports/Test_Project/Test_Project.chm"><img
src="images/Icon_CHM.gif" width="16" height="16" alt="View Sample Output (HTML Help format)"
border="0" align="absmiddle"></a> <a
href="http://www.winnershtriangle.com/w/SQLDocumentationTool/Reports/Test_Project/Test_Project.chm" title="View Sample Output (HTML Help format)">View Sample
Output</a> (HTML Help format).<br>
   <a
href="http://www.winnershtriangle.com/w/SQLDocumentationTool/Reports/Test_Project/Default.htm"><img
src="images/Icon_HTML.gif" width="16" height="16" alt="View Sample Output (HTML Format)"
align="absmiddle" border="0"></a> <a
href="http://www.winnershtriangle.com/w/SQLDocumentationTool/Reports/Test_Project/Default.htm"
target="_blank" title="View Sample Output (HTML Format)">View Sample Output</a> (HTML Format).<br>
   <a
href="http://www.brettb.com/redirector.asp?URL=downloads/SQLDocumentationToolTrial.zip"><img
src="images/Icon_Download.gif" width="18" height="20" alt="Download Trial Version"
border="0" align="absmiddle"></a> <a
href="http://www.brettb.com/redirector.asp?URL=downloads/SQLDocumentationToolTrial.zip" title="Download Trial Version">Download
Trial Version</a> (10.3Mb ZIP file).</td>
</tr>
</table>
<br>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td width="100%" valign="top"><a
href="http://www.winnershtriangle.com/w/Products.IndexServerCompanion.asp" target="_blank" title="Index Server Companion"><strong>Index Server Companion</strong></a></td>
</tr>
<tr>
<td width="100%" valign="top">
The Index Server Companion is a Windows application that extends the functionality of Microsoft Index Server so that it is able to index content from remote websites and also from ODBC databases. As such it can be used as a low cost alternative to Site Server 3.0 Search.

</td>
</tr>
<tr>
<td width="100%" valign="top">
   <a
href="http://www.brettb.com/redirector.asp?URL=downloads/IndexServerCompanionDocumentation.zip"><img
src="images/Icon_CHM.gif" width="16" height="16" alt="View Product Documentation"
border="0" align="absmiddle"></a> <a
href="http://www.brettb.com/redirector.asp?URL=downloads/IndexServerCompanionDocumentation.zip" title="View Product Documentation">View Product
Documentation</a> (119K ZIP file).<br>
   <a
href="http://www.winnershtriangle.com/w/Products.IndexServerCompanion.SampleSearch.asp"><img
src="images/Icon_HTML.gif" width="16" height="16" alt="Try Sample Search Facility"
align="absmiddle" border="0"></a> <a
href="http://www.winnershtriangle.com/w/Products.IndexServerCompanion.SampleSearch.asp"
target="_blank" title="View Sample Search Facility">Try Sample Search Facility</a>.<br>
   <a
href="http://www.brettb.com/redirector.asp?URL=downloads/IndexServerCompanion_EvaluationVersion.zip"><img
src="images/Icon_Download.gif" width="18" height="20" alt="Download Trial Version"
border="0" align="absmiddle"></a> <a
href="http://www.brettb.com/redirector.asp?URL=downloads/IndexServerCompanion_EvaluationVersion.zip" title="Download Trial Version">Download
Trial Version</a> (1.7Mb ZIP file).</td>
</tr>
</table>
<br>

<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td width="100%" valign="top"><a
href="http://www.winnershtriangle.com/w/Products.TheWebsiteUtility.asp" target="_blank" title="The Website Utility"><strong>The Website Utility</strong></a></td>
</tr>
<tr>
<td width="100%" valign="top">The Website Utility examines websites for errors and
areas that need to be optimised for search engines by using a built in web crawling engine.
Errors checked for include broken or moved hyperlinks, missing page titles and missing meta tags.
It also generates HTML for use in creating website site maps (table of contents pages - like <a href="http://authors.aspalliance.com/brettb/TableOfContents.asp" title="Example Table of Contents">this one</a>), and is
able to create both client-side <a href="http://www.winnershtriangle.com/w/Products.TheWebsiteUtility.JavaScriptSearchEngine.asp" title="JavaScript Search Engine">JavaScript Search Engines</a> and server-side <a href="http://www.winnershtriangle.com/w/Products.TheWebsiteUtility.ASPSearchEngine.asp" title="JavaScript Search Engine">ASP Search Engines</a> for a website.</td>
</tr>
<tr>
<td width="100%" valign="top">
   <a href="http://www.winnershtriangle.com/w/TheWebsiteUtility/Reports/ActiveHealthImaging/ActiveHealthImaging.chm"><img
src="images/Icon_CHM.gif" width="16" height="16" alt="View Sample Output (HTML Help format)"
border="0" align="absmiddle"></a> <a
href="http://www.winnershtriangle.com/w/TheWebsiteUtility/Reports/ActiveHealthImaging/ActiveHealthImaging.chm" title="View Sample Output (HTML Help format)">View Sample
Output</a> (HTML Help format).<br>
   <a
href="http://www.winnershtriangle.com/w/TheWebsiteUtility/Reports/ASPAlliance/default.htm"><img
src="images/Icon_HTML.gif" width="16" height="16" alt="View Sample Output (HTML Format)"
align="absmiddle" border="0"></a> <a
href="http://www.winnershtriangle.com/w/TheWebsiteUtility/Reports/ASPAlliance/default.htm"
target="_blank" title="View Sample Output (HTML Format)">View Sample Output</a> (HTML Format).<br>
   <a
href="http://www.brettb.com/redirector.asp?URL=downloads/TheWebsiteUtilityTrial.zip"><img
src="images/Icon_Download.gif" width="18" height="20" alt="Download Trial Version"
border="0" align="absmiddle"></a> <a
href="http://www.brettb.com/redirector.asp?URL=downloads/TheWebsiteUtilityTrial.zip" title="Download Trial Version">Download
Trial Version</a> (3Mb ZIP file).</td>
</tr>
</table>

</div>

</td>
<td width="100" valign="top" align="left">
<script type="text/javascript"><!--
google_ad_client = "pub-7044749527879330";
google_ad_width = 120;
google_ad_height = 600;
google_ad_format = "120x600_as";
google_ad_channel ="0936536850";
google_ad_type = "text_image";
google_color_border = "CCCCCC";
google_color_bg = "FFFFFF";
google_color_link = "0000FF";
google_color_url = "DD3333";
google_color_text = "000000";
google_alternate_ad_url = "http://www.brettb.com/NoGoogleAds.asp";
//--></script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>

<a href="http://www.winnershtriangle.com/w/Products.ASPDocumentationTool.asp"><img
src="http://authors.aspalliance.com/brettb/images/ASPDocumentationTool_1.gif" width="120"
height="60" alt="ASP Documentation Tool - Free Trial Available!" border="0"></a>
<br>

<p align="center"><a href="http://www.jdoqocy.com/lp75gv30v2IKPMSMNLIRLRPNMQ" target="_blank" onmouseover="window.status='http://www.lunarpages.com';return true;" onmouseout="window.status=' ';return true;">
<img src="http://www.awltovhc.com/3a74r6Az42OQVSYSTROXRXVTSW" alt="1000MB and 40GB for $7.95 a month!" border="0"></a></p>

<iframe marginwidth="0" marginheight="0" src="http://rcm.amazon.com/e/cm?t=brettbcom&o=1&p=10&l=st1&mode=books&search=asp&=1&fc1=&lc1=<1=&bg1=&f=ifr" width="130" height="460" border="0" frameborder="0" style="border:none;" scrolling="no"></iframe>
</td>
</tr>
</table>
</center></div>
<div align="center"><center>

<table border="0" cellpadding="2" cellspacing="0" width="802"
style="border: 1px solid rgb(0,0,0)">
<tr>
<td class="TDFooter">  <a href="toc.asp" title="Site Map">Site Map</a></td>
<td class="TDFooter"><p align="right">All content is © 1995 - 2006 Brett Burridge</td>
</tr>
</table>
</center></div>
</body>
</html>