Computer Science Canada Programming C, C++, Java, PHP, Ruby, Turing, VB   Username:   Password: Wiki   Blog   Search   Turing   Chat Room  Members
[Tutorial] 3 Step Guide To Random Integers
Author Message
Genesis

Posted: Fri Sep 24, 2004 10:06 pm   Post subject: [Tutorial] 3 Step Guide To Random Integers

I spent half an hour today trying to get this to work. (I haven't used VB in a while.) So I figured I'd write a very short and easy tutorial on it.

Though there are other, better ways to do this. (Through the use of functions and such, which I may post a tutorial on later.) This way is the simpelest.

Step 1: Create a button, and change the caption to something like "Generate".

Step 2: Create a text box, and delete the default text to make it blank.

Step 3: Double click on the button you created and the code window should appear. Type this:

 code: Private Sub Command1_Click() Randomize Text1 = Int(Rnd * 10) End Sub

Where Text1 is the name of the text box you created on the Form, Int is the type of number you want to generate, and (Rnd * 10) is a number between 1 and 10.

That's all I can think of to put here. Should you need more help, feel free to post.

Tony

Posted: Sun Sep 26, 2004 12:25 am   Post subject: (No subject)

I'd just like to add that Rnd generates a random value between 0 and 1. *10 operation just extends the range of the random output.

Int() typecasts the value into integer. I dont remember how Visual Basic works. If it drops the floatpoint (as C++ does) you might not be able to generate 10 that way. So just test.
Tony's programming blog. DWITE - a programming contest.
Acid

Posted: Sun Sep 26, 2004 11:09 am   Post subject: (No subject)

Int() makes it drop the floatpoint. I think you'd have to go * 11 to get one between 1 and 10
Brightguy

Posted: Mon Sep 27, 2004 6:50 am   Post subject: Re: [Tutorial] 3 Step Guide To Random Integers

*11 would make it go between 0 and 10. The number returned by Rnd is greater than or equal to zero, but less than one.

Use this formula to generate a random number from the range lngLowerBound to lngUpperBound:
 code: Private Function Random(lngLowerBound As Long, lngUpperBound As Long)      Random = Int(Rnd * (lngUpperBound - lngLowerBound + 1) + lngLowerBound) End Function

You could either put Randomize in the function, or just in the Form_Load.
Acid

Posted: Mon Sep 27, 2004 1:34 pm   Post subject: Re: [Tutorial] 3 Step Guide To Random Integers

Brightguy wrote:
*11 would make it go between 0 and 10. The number returned by Rnd is greater than or equal to zero, but less than one.

Right. Your way made much more sense.
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

Page 1 of 1  [ 5 Posts ]
 Jump to:  Select a forum  CompSci.ca ------------ - Network News - General Discussion     General Forums   -----------------   - Hello World   - Featured Poll   - Contests     Contest Forums   -----------------   - DWITE   - [FP] Contest 2006/2008   - [FP] 2005/2006 Archive   - [FP] 2004/2005 Archive   - Off Topic     Lounges   ---------   - User Lounge   - VIP Lounge     Programming -------------- - General Programming     General Programming Forums   --------------------------------   - Functional Programming   - Logical Programming   - C     C   --   - C Help   - C Tutorials   - C Submissions   - C++     C++   ----   - C++ Help   - C++ Tutorials   - C++ Submissions   - Java     Java   -----   - Java Help   - Java Tutorials   - Java Submissions   - Ruby     Ruby   -----   - Ruby Help   - Ruby Tutorials   - Ruby Submissions   - Turing     Turing   --------   - Turing Help   - Turing Tutorials   - Turing Submissions   - PHP     PHP   ----   - PHP Help   - PHP Tutorials   - PHP Submissions   - Python     Python   --------   - Python Help   - Python Tutorials   - Python Submissions   - Visual Basic and Other Basics     VB   ---   - Visual Basic Help   - Visual Basic Tutorials   - Visual Basic Submissions     Education ----------- - Student Life   Graphics and Design ----------------------- - Web Design     Web Design Forums   ---------------------   - (X)HTML Help   - (X)HTML Tutorials   - Flash MX Help   - Flash MX Tutorials   - Graphics     Graphics Forums   ------------------   - Photoshop Tutorials   - The Showroom   - 2D Graphics   - 3D Graphics     Teams ------ - dTeam Public

 Style: Appalachia blueSilver eMJay subAppalachia subBlue subCanvas subEmjay subGrey subSilver subVereor Search: