[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


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

Posted on 2005-05-04
Medium Priority
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?
Question by:sonicamps
  • 2
LVL 10

Accepted Solution

heintalus earned 2000 total points
ID: 13932477
You could try using the Round function i.e.

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

LVL 86

Expert Comment

by:Mike Tomlinson
ID: 13932754
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)

LVL 86

Expert Comment

by:Mike Tomlinson
ID: 13933241
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.


Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Kraeven
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…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

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

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

Join & Ask a Question