October 23, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Discovering Visual Basic .NET: Using Functions and Arguments

  • December 30, 2004
  • By Bill Hatfield
  • Send Email »
  • More Articles »

Using Functions in Formulas with Rnd and Int

One of the handier things about functions and the way they return their values is that you can use them right in the middle of larger formulas. To illustrate how this works, I'm going to introduce you to two new functions: Rnd and Int.

Run the following program and look at the results:

Imports System
Imports Microsoft.VisualBasic

Module RandomNumber
  Public Sub Main()
    Dim RandNum As Single
    Randomize
    RandNum = Rnd
    Console.WriteLine(RandNum)
  End Sub
End Module

Now, run the program again. And again. If everything went well, you see a new number appear each time you run the program. Because the number is random, I don't know exactly what number you'll see, and you get a different number every time. But the number probably looks something like this:

0.1525385

The Randomize statement and the Rnd function work together to generate random numbers. Randomize needs to appear in any program where you plan to use the Rnd function—before you use it. Randomize gets the ball rolling.

Then, Rnd returns decimal numbers that are greater than 0 but less than 1. You get a different number each time you run the program. Random numbers come in very handy when you're creating games and some types of cool graphics.

Note: The computer actually goes through a complex formula to generate a "random" number. But, the computer needs a number to stick into the formula to kick it off—what programmers call a seed value. If you don't use the Randomize statement, the computer always starts with the same seed. And, therefore, the computer ends up with the same series of "random" numbers every single time. Not very random, right? So, Randomize goes to the system clock and gets the numbers that it uses as the seed for the complex formula. That way, the seed and the random numbers are different every time you run your program.

But, these random numbers between 0 and 1 aren't very useful, are they? For example, if you wanted to create a program that simulates drawing cards or rolling dice, you would want numbers between 1 and 13 or between 1 and 6. Unfortunately, VB.NET doesn't provide any other random-number functions. So, you have to use this one and tweak it a little. Take a look at this code:

RandNum = (Rnd * 6) + 1

This line calls the Rnd function, which returns a decimal value between 0 and 1. Then, that value is multiplied by 6. Now, you have a decimal value between 0 and 5. So you add 1 to get a decimal value between 1 and 6. That number is then assigned to the RandNum variable.

The parentheses help VB.NET decide which calculations it does first. In this case, it multiplies 6 times the number returned from Rnd first because that's in parentheses. (Actually, in this case, even if the parentheses weren't there, VB.NET would always do the multiplication before the addition, but the parentheses make it clearer.)

Try plugging the preceding line of code into your program in place of the RandNum = Rnd line. Save it, compile, and run. See what you get. You'll should see a number that looks something like this:

4.167642

Run it again. You should continue to see numbers like that between 1 and 6. Great! That's what you wanted, right? Well, yes, except for that annoying decimal part of the number. If only you could just chop off that decimal part.

And, of course, you can. Just use a function called Int, which stands for integer. Just as the Integer data type enables you to create variables that hold whole numbers, so the Int function takes a number with a decimal part (like the ones you're getting here) and returns an integer. The Int function expects one argument: a number with a decimal part. Int returns the exact same number with the decimal part chopped off.

Notice I didn't say that Int rounds the number off—it doesn't. Int simply, unceremoniously, chops off any decimal part and returns the whole number.

Replace the RandNum = (Rnd * 6) + 1 line in your program with this new one:

RandNum = Int(Rnd * 6) + 1

Save, compile, and run your program again. Again, I can't predict exactly what number you'll see, but it should look something like this:

2

Run it again. Each time, you should see whole numbers between 1 and 6. Perfect! Now you can create random numbers between 1 and any number you like.

Want to simulate pulling cards off a deck? Use this line:

RandNum = Int(Rnd * 13) + 1

The 1 is an Ace, 11 through 13 are Jacks through Kings, and the rest are number cards. Easy enough.

Want to simulate flipping a coin? Use this line:

RandNum = Int(Rnd * 2) + 1

This code generates either a 1 or a 2. Call 1 heads and 2 tails and you're ready to go!

Finally, if you want to generate random percentages, use this line:

RandNum = Int(Rnd * 100) + 1




Page 2 of 5



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel