WorldofASP.NET : ASP.NET Directory, Tutorial, Hosting, and Source Code
You are 1 of 128 users


WorldofASP.NET >> General .NET >> Applications

How to Generate Random Numbers in ASP.NET and .NET Framework

Generating Random Number in ASP.NET and .NET Framework
Published Date : 03 Sep 2007
Author : James Douglas
Language : VB.NET,C#
Platform : Wins,.NET
Technology : Visual Studio,ASP.NET
Views : 6549
Rating : (0 votes so far)



Introduction

Random number generation is a common programming endeavor, but many developers are unaware of the true nature of the random numbers generated by a system. In this article I would explain about how to generating a random numbers and how to use them in your .NET applications

Generating random numbers is often a hot topic for programmers. The reason is that seemingly random numbers are most often not genuinely random. This is due to the deterministic algorithm utilized by a platform like .NET. Deterministic systems leave nothing to chance; they're lawful out of necessity; and they conform to the ideal of a machine in which wear and tear and mechanical failures are absent. Modern computers are conceived as deterministic machines.

The Random class

The .NET Framework provides the Random class, which allows you to generate a number or a series of numbers to be used accordingly. You may be wondering why .NET bothers to include the Random class. The system may be deterministic, but it can be utilized to create values that meet a need. The result is pseudo-random numbers, but you must be careful to properly use the Random object to ensure different values.

Planting a seed

Any pseudo-random number formula depends on the seed value to start the sequence. If you start with the same seed, you will get the same sequence of values from the formula. The .NET Framework's Random class allows you to specify the seed value or allow the system to generate it.

To create a random and unpredictable sequence, the seed must be a truly random number. To get this truly random number for the seed, most programs use the current date and time, converted to an integer value. The theory is this is a different number every time the code executes, but the speed of modern computers negates this assumption.

The next code snippet creates an instance of the Random class in VB.NET:

Dim randObj As New Random(0)
Console.WriteLine(randObj.Next().ToString())

This code creates a Random object using a seed value of zero. The problem with this approach is that it creates the same value(s) every time it is used since the seed value is a constant. It produces the number 1559595546 on my machine. The same code in C# follows:

Random randObj = new Random(0);
Console.WriteLine(randObj.Next().ToString());

The Random class does provide an empty constructor. It initializes a new instance of the Random class, using a time-dependent default seed value. That is, it uses the system clock. Therefore, you could rewrite the previous code using this approach:

Random randObj = new Random();
Console.WriteLine(randObj.Next().ToString());

You may run this code a few times and notice that values are not repeated. Let's take a closer look at the methods available in the Random class.

Random class members

The Random class includes various methods for creating and accessing values. Here is a subset of these methods:

  • Next: Returns a random number generated using the seed value.
  • NextBytes: Fills the elements of a specified array of bytes with random numbers.
  • NextDouble: Returns a random number between 0.0 and 1.0.

All of these methods are used to access numbers generated by the object. They should be used in succession to work with multiple random numbers or a series of random numbers. This is due to the fact that two or more Random objects initialized with the same seed will produce the same series.

The Next method

The Next method supports three signatures. We've used the first approach where no parameter is used and a random number is returned. The second approach accepts one parameter that signals a maximum value, thus the random number returned is less than that value. The last signature accepts both minimum and maximum values, so a value is returned that is between these values. The next VB.NET code sample demonstrates each:

Dim randObj As New Random
Console.WriteLine(randObj.Next().ToString())
Console.WriteLine(randObj.Next().ToString())
Console.WriteLine(randObj.Next().ToString())
' Use a maximum constraint
Console.WriteLine(randObj.Next(10).ToString())
Console.WriteLine(randObj.Next(100).ToString())
Console.WriteLine(randObj.Next(1000).ToString())
' Use minimum and maximum constraints
Console.WriteLine(randObj.Next(1, 100).ToString())
Console.WriteLine(randObj.Next(101, 1000).ToString())
Console.WriteLine(randObj.Next(1001, 10000).ToString())

The C# equivalent follows:

Random randObj = new Random();
Console.WriteLine(randObj.Next().ToString());
Console.WriteLine(randObj.Next().ToString());
Console.WriteLine(randObj.Next().ToString());
// Use a maximum constraint
Console.WriteLine(randObj.Next(10).ToString());
Console.WriteLine(randObj.Next(100).ToString());
Console.WriteLine(randObj.Next(1000).ToString());
// Use minimum and maximum constraints
Console.WriteLine(randObj.Next(1, 100).ToString());
Console.WriteLine(randObj.Next(101, 1000).ToString());
Console.WriteLine(randObj.Next(1001, 10000).ToString());

The NextBytes method

The NextBytes method allows you to populate a byte array with random values. This allows you to quickly assemble a batch of values. The next VB.NET example shows how this may be used.

Dim randObj As New Random
Dim randArray(20) As Byte
randObj.NextBytes(randArray)
Dim i As Integer
For i = 0 To (randArray.Length â€" 1)
Console.WriteLine(randArray(i))
Next i

The result is the byte array populated with numbers generated by repeated calls to the Random object. The C# equivalent follows:

Random randObj = new Random();
Byte[] randArray = new Byte[20];
randObj.NextBytes(randArray);
for (inti = 0; i < randArray.Length; i++)
Console.WriteLine(randArray[i]);

The NextDouble method

The NextDouble method returns a double value as opposed to an integer. The value is in the range of 0.0 and 1.0.

Randomness and security

One application of random numbers is security. Many cryptography algorithms depend upon random numbers; therefore, the algorithms are only as strong as the random numbers. For this reason, .NET's basic Random class is not a good choice for security. However, .NET'sSystem.Security.Cryptography namespace does include the RNGCryptoServiceProvider class for working with a cryptographic Random Number Generator (RNG). The effectiveness of it is debatable and beyond the scope of this article.

Know the application

It is a common assumption that random numbers are really random, but this just isn't true. The algorithm modern computers use makes these numbers often predictable, but values like the system clock can be used to generate pseudo-random numbers. Keep this background information handy when random numbers are a necessity in your application.


    Tag Cloud
      upload file progress bar asp.net   xhtml-mp asp.net   asp.net delegate   asp.net httpwebrequest   smart device application   generate random number asp.net   datalist control   asp.net pass data from one page to another   httpwebresponse   asp.net activate user   frames image gallery thumbnails asp.net   asp.net tooltip   asp.net upload ajax progress   formview displaying inserted data   contact us asp.net   cannot start service from the command line or a debugger   asp.net random number   httpwebrequest asp.net c#   checkboxlist datasource   asp.net encryption   httpwebrequest   formview asp.net   edit update insert in a gridview c#   create httpwebrequest   encrypt querystring in asp.net   httpwebrequest .net   "javascript in asp.net"   tooltip asp.net   asp httpwebrequest   asp.net listbox control   asp.net file upload progress   asp.net cookies encryption   httpwebrequest vb.net   asp.net cookie shopping cart   cannot start service from the command line or a debugger. a windows service must   httpwebrequest create   feedback form in asp.net   asp.net url parameters   httpwebrequest httpwebresponse   asp.net file upload progress bar   httpmodule httphandler   ajax updatepanel button   form view in asp.net   httpwebrequest in asp.net 2.0





    Other Related and Popular Articles :

    Creating Windows Services in .NET
    Basic example and tutorial about Windows Services in .NET

    Creating and Consuming XML Web Services
    Creating a Simple Web Method and Using it on a Client Application


    Author Profile : James Douglas

    I work in a Software House Company in Malaysia (Kuala Lumpur) and I am MCP Certified in C# and Web Application course.
    I originally started my programming in Java but later on changed to Microsoft platform because of the simplicity and ease of use.
    I love .NET programming and am doing it almost every day now.

    Click here to view Author Profile


    How would you rate the quality of this content?
    Poor Excellent

    Comments

    Leave New Comments


    Article Content copyright by James Douglas
    Everything else Copyright © by WorldofASP.NET 2008

    Category
    .NET 3.5
    AJAX and ATLAS
    ASP.NET
    C# Programming
    Classic ASP
    Enterprise Systems
    General .NET
    VB.NET Programming
    Announcements
    Earn Cash by writing an article or review
    For more info Click here







    Legend : - Within 3 Days - Within 6 Days - Within 9 Days

    Home | Add Resources | Sponsored Listings | Advertise with Us | SiteMap 1 | SiteMap 2 | Link To Us | Contact Us
    © 2002-2008 Worldofasp.net ASP.NET Directory, Hosting and Tutorials | All rights reserved
    Our Partners : ASP.NET Web Hosting | Windows Web Hosting | ASP.NET Hosting | Phone Card | PHP Directory | Bangkok Hotels |Calling Card