r/visualbasic • • Feb 24 '16

I need to generate 3 random numbers within a range (0<X<10), and assign those numbers to variables, all inside a sub-procedure. Every attempt at researching "making a random number" brings me to generating a number by pressing a button. Can anyone help me with this problem?

This is the full text of my problem, for added context.

Triangle inequality theorem states that the sum of two sides of a triangle must be greater than the third side. If this is true for all three combinations, then you will have a valid triangle.

Write a VB application that will randomly generate the sides of 5 triangles and calculate the perimeter and area of each triangle. When the user clicks btnTrianglesbutton, it should complete the following tasks:

(a)Call getTriangleSides(...) sub-procedure within click event procedure of btnTriangles. This procedure will randomly generate three sides of a triangle.Each side of a triangle must be greater than zero and less than 10.

(b)Call getProperties(...) sub-procedure within click event procedure of btnTriangles. This procedure will calculate the perimeter and area of the triangle.

(c)Display sides of 5 triangles with perimeter and area in tabular format within click event procedure of btnTriangles. Format numbers to one decimal place.Add clear button in your application.

6 Upvotes

2 comments sorted by

3

u/JakDrako Feb 24 '16

Assuming VB.Net, you can use the "System.Random" object to create your random numbers. Here is a short example:

Sub Main
	
	Dim rnd = New System.Random ' You only need 1 of those
	
	' get our numbers
	For i = 1 To 3

		' In "rnd.Next" the lower bound is inclusive, 
        ' and upper bound exclusive,
		' so 1, 11 will generate between 1..10
        Dim number = rnd.Next(1, 11)

		Console.WriteLine(number)  
	Next
	
End Sub

1

u/uni_and_internet Feb 25 '16

Thank you so much!