Solved

# How do I use the Rnd function to produce a whole number?

Posted on 2005-05-04
191 Views
Last Modified: 2011-04-14
I currently have this code:

Dim High As Integer
Dim Low As Integer

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
High = 12
Low = 1
TextBox1.Text = ((High - Low + 1) * Rnd() + Low)
End Sub

When I click on button1, I get a number between 1 and 12, which is what I want, but it is returned in decimal form..such as 2.334989 or 9.459878.  Any suggestions on how I can get the textbox to display a whole number such as 4 or7?
0
Question by:sonicamps
3 Comments

LVL 10

Accepted Solution

You could try using the Round function i.e.

TextBox1.Text = Math.Round(((High - Low + 1) * Rnd() + Low),0).ToString

0

LVL 85

Expert Comment

Like so...notice that we must add one to the second parameter:

Dim High As Integer = 12
Dim Low As Integer = 1
Dim r As New Random
TextBox1.Text = r.Next(Low, High + 1)

0

LVL 85

Expert Comment

Just FYI, the Rnd() function you are using is a carryover from the VB6 days (part of the Microsoft.VisualBasic namespace) and may not be available in future versions of VB.Net.  The .Net supplied method is to use the Random() class as I have demonstrated.

~IM
0

## Join & Write a Comment Already a member? Login.

Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

#### 746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

#### Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!