Random Numbers and Tic Tac Toe with Visual Basic .NET

The holidays are a time for fun. Learn about random numbers from Paul Kimmel's latest Visual Basic Today column from CodeGuru.com. Includes a Tic Tac Toe game!

provided by: 
Originally published at Internet.com


If you are a little like I am you will be doing something with your computer even during the holidays. (More than likely I will be playing Red Alert 2 with the kids, but I may sneak in a little writing.) For you workaholics, I have included a little holiday fare. This way you will be able to do something constructive, yet be able to tear yourself away when company comes knocking on the door.

Random numbers are commonly employed in applications for a wide variety of reasons. In this week's letter let's take a quick look at the Random class in the Common Language Runtime.

The System Namespace



The System namespace is a root namespace in the Common Language Runtime (CLR). One of the classes in the System namespace is the Random class. (That's right! Random numbers are encapsulated in a class in .NET.) Like VB6 random numbers are based on a mathematical algorithm from a finite set of numbers. The result is that you get pseudo-random numbers, but these numbers are random enough for many practical purposes (just not the lottery).

Random numbers in VB.NET can be initialized with or without a seed value and can be constrained to a range of numbers. If the same seed is used then the generated number sequence will be identical. A good strategy is to seed the random numbers using a unique value like the time. Table 1 describes the members of the Random class and is followed by some code examples demonstrating how to use the Random class. Members of the System.Random class. Member Name Description New() Constructs instance of Random Class. New(ByVal Seed As Integer) Constructs instance of Random class using Seed value. Next() As Integer Returns next integer in the random number sequence. Next(ByVal maxValue As Integer) As Integer Returns a positive integer less than maxValue. Next(ByVal minValue As Integer, ByVal maxValue As Integer) As Integer Returns a number greater than or equal to minValue and less than or maxValue. NextBytes(ByVal buffer() As Byte) Returns an array of random bytes greater than or equal to 0 and less than Byte.MaxValue. NextDouble() As Double Returns a random Double between 0.0 and 1.0...

Read article at Internet.com site
Related Articles
- Custom MSBuild Task Development with Visual C++
Visual C++ 2010 sees the integration of MSBuild with Visual C++. MSBuild is Microsoft's common build engine and one of its key design goals is the ability to support a high-degree of flexibility in customizing the build process. See how a custom MSBuild task can be built in Visual C++, and how the task can be used to customize the build process of other Visual C++ 2010 projects.
- Microsoft Scripting Runtime in Visual Basic 6
- Automating Repetitive Tasks in Visual Studio
- Capturing CommandBar Events in VB Add-Ins
- Adding Standard Deviation to LINQ
- Book Review: ASP.NET For Dummies
- Visual Basic .NET as a Fully Object-Oriented Language
- MSBuild Features for Visual C++
- Visual Basic .NET Attributes
- Encrypting a File Using .NET
Regional Articles
- Random Numbers and Tic Tac Toe with Visual Basic .NET Alabama
- Random Numbers and Tic Tac Toe with Visual Basic .NET Alaska
- Random Numbers and Tic Tac Toe with Visual Basic .NET Arizona
- Random Numbers and Tic Tac Toe with Visual Basic .NET Arkansas
- Random Numbers and Tic Tac Toe with Visual Basic .NET California
- Random Numbers and Tic Tac Toe with Visual Basic .NET Colorado
- Random Numbers and Tic Tac Toe with Visual Basic .NET Connecticut
- Random Numbers and Tic Tac Toe with Visual Basic .NET DC
- Random Numbers and Tic Tac Toe with Visual Basic .NET Delaware
- Random Numbers and Tic Tac Toe with Visual Basic .NET Florida
- Random Numbers and Tic Tac Toe with Visual Basic .NET Georgia
- Random Numbers and Tic Tac Toe with Visual Basic .NET Hawaii
- Random Numbers and Tic Tac Toe with Visual Basic .NET Idaho
- Random Numbers and Tic Tac Toe with Visual Basic .NET Illinois
- Random Numbers and Tic Tac Toe with Visual Basic .NET Indiana
- Random Numbers and Tic Tac Toe with Visual Basic .NET Iowa
- Random Numbers and Tic Tac Toe with Visual Basic .NET Kansas
- Random Numbers and Tic Tac Toe with Visual Basic .NET Kentucky
- Random Numbers and Tic Tac Toe with Visual Basic .NET Louisiana
- Random Numbers and Tic Tac Toe with Visual Basic .NET Maine
- Random Numbers and Tic Tac Toe with Visual Basic .NET Maryland
- Random Numbers and Tic Tac Toe with Visual Basic .NET Massachusetts
- Random Numbers and Tic Tac Toe with Visual Basic .NET Michigan
- Random Numbers and Tic Tac Toe with Visual Basic .NET Minnesota
- Random Numbers and Tic Tac Toe with Visual Basic .NET Mississippi
- Random Numbers and Tic Tac Toe with Visual Basic .NET Missouri
- Random Numbers and Tic Tac Toe with Visual Basic .NET Montana
- Random Numbers and Tic Tac Toe with Visual Basic .NET Nebraska
- Random Numbers and Tic Tac Toe with Visual Basic .NET Nevada
- Random Numbers and Tic Tac Toe with Visual Basic .NET New Hampshire
- Random Numbers and Tic Tac Toe with Visual Basic .NET New Jersey
- Random Numbers and Tic Tac Toe with Visual Basic .NET New Mexico
- Random Numbers and Tic Tac Toe with Visual Basic .NET New York
- Random Numbers and Tic Tac Toe with Visual Basic .NET North Carolina
- Random Numbers and Tic Tac Toe with Visual Basic .NET North Dakota
- Random Numbers and Tic Tac Toe with Visual Basic .NET Ohio
- Random Numbers and Tic Tac Toe with Visual Basic .NET Oklahoma
- Random Numbers and Tic Tac Toe with Visual Basic .NET Oregon
- Random Numbers and Tic Tac Toe with Visual Basic .NET Pennsylvania
- Random Numbers and Tic Tac Toe with Visual Basic .NET Rhode Island
- Random Numbers and Tic Tac Toe with Visual Basic .NET South Carolina
- Random Numbers and Tic Tac Toe with Visual Basic .NET South Dakota
- Random Numbers and Tic Tac Toe with Visual Basic .NET Tennessee
- Random Numbers and Tic Tac Toe with Visual Basic .NET Texas
- Random Numbers and Tic Tac Toe with Visual Basic .NET Utah
- Random Numbers and Tic Tac Toe with Visual Basic .NET Vermont
- Random Numbers and Tic Tac Toe with Visual Basic .NET Virginia
- Random Numbers and Tic Tac Toe with Visual Basic .NET Washington
- Random Numbers and Tic Tac Toe with Visual Basic .NET West Virginia
- Random Numbers and Tic Tac Toe with Visual Basic .NET Wisconsin
- Random Numbers and Tic Tac Toe with Visual Basic .NET Wyoming

Rss   Delicious   Digg   Add To My Yahoo   Add To My Google   Bookmark   Search Plugin

Topics:
Architecture & Design Languages & Tools Project Management Web Services
Database Microsoft & .NET Security Wireless
Java Open Source Techniques XML