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

Posted on 2005-05-04
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
    LVL 10

    Accepted Solution

    You could try using the Round function i.e.

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

    LVL 85

    Expert Comment

    by:Mike Tomlinson
    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 85

    Expert Comment

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

    Courses: Start Training Online With Pros, Today

    Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

    Join & Write a Comment

    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…
    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.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now