Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 [Tutorial] 3 Step Guide To Random Integers
Index -> Programming, Visual Basic and Other Basics -> Visual Basic Tutorials
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Genesis




PostPosted: 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.
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: 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.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Acid




PostPosted: 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




PostPosted: 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




PostPosted: 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:   
   Index -> Programming, Visual Basic and Other Basics -> Visual Basic Tutorials
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: