recieve upper bound and lower bound of a range
Is there anyone who can tell me the code for the below?
Receive a lower bound and an upper bound of a range. Also receive a number of random numbers to generate.
You must validate
1) the numbers are integers
2) the lower bound is less than or equal to the upper bound
3) the number to generate is positive (i.e., > 0).
Generate this many numbers in the specified range and keep track of how many of each are generated in an array of counters.
Once all the numbers have been generated, display in a List Box the number, the raw count data, and the percentage of
how many times the number was generated displayed with two decimal places of precision.
Re: recieve upper bound and lower bound of a range
Moved From The CodeBank
There are a great many of us who "can" tell you what the code is, but we won't.
You try...and if you have problem, post your problem and we will help you solve it.
Here is a starting point I would suggest...look at the textbooks or notes you have taken for the class you are taking.
Re: recieve upper bound and lower bound of a range
Hi shivani...:wave:
I think this will get you started: http://www.vbforums.com/showpost.php...80&postcount=3
While you develop the program, if you got stuck at any point feel free to ask us. We're happy to help you(only if you show some effort from your side) . :wave:
Re: recieve upper bound and lower bound of a range
That is one ugly use of a listbox.
Re: recieve upper bound and lower bound of a range
Quote:
Originally Posted by
sha_shivani
Is there anyone who can tell me the code for the below?
Receive a lower bound and an upper bound of a range. Also receive a number of random numbers to generate.
You must validate
1) the numbers are integers
2) the lower bound is less than or equal to the upper bound
3) the number to generate is positive (i.e., > 0).
Generate this many numbers in the specified range and keep track of how many of each are generated in an array of counters.
Once all the numbers have been generated, display in a List Box the number, the raw count data, and the percentage of
how many times the number was generated displayed with two decimal places of precision.
I already coded this but i am not sure how to Receive a lower bound and an upper bound of a range.Currently i am getting the values from the textbox in which i enter value.As the assignment says receive a lower bound and an upper bound of a range do i need to hardcode values inside code?
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Initialize the random # generator.
Randomize()
End Sub
Public Function Rand(ByVal Low As Decimal, _
ByVal High As Decimal) As Decimal
Low = txtLowerBound.Text
High = txtUpeprBound.Text
Rand = Int((High - Low + 1) * Rnd()) + Low
txtGenerate.Text = Rand
End Function
Private Sub btnCreateReport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreateReport.Click
Dim Low As Decimal, High As Decimal
Rand(Low, High)
End Sub
End Class
Re: recieve upper bound and lower bound of a range
That is VB.Net code... Thread moved from the 'VB6 and Earlier' forum to the 'VB.Net' (VB2002 and later) forum
Re: recieve upper bound and lower bound of a range
I have rearranged the code:
vb Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
'Initialize the random # generator.
Randomize()
End Sub
Public Function Rand(ByVal Low As Integer, _
ByVal High As Integer) As Decimal
Return (Int((High - Low + 1) * Rnd()) + Low)
End Function
Private Sub btnCreateReport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreateReport.Click
Dim Low As Integer, High As Integer
'~~~ Conversion to Integer
Integer.TryParse(txtLowerBound.Text, Low)
Integer.TryParse(txtUpperBound.Text, High)
'~~~ Call the function, passing LOWER and UPPER bound(range), and display the returned value
txtGenerate.Text = Rand(Low, High).ToString
End Sub
End Class
Assignment is not complete. You need to take care of the rest of the things. :thumb:
Edit:
Changed the datatype from Decimal to Integer. (see the below post of jmcilhinney)
Re: recieve upper bound and lower bound of a range
The assignment specifically states that the numbers are integers so why are you using Decimal? Integer.TryParse will tell you whether the input is an Integer and, if it is, give you the value.
Re: recieve upper bound and lower bound of a range
Quote:
Originally Posted by
akhileshbc
I have rearranged the code:
vb Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
'Initialize the random # generator.
Randomize()
End Sub
Public Function Rand(ByVal Low As Integer, _
ByVal High As Integer) As Decimal
Return (Int((High - Low + 1) * Rnd()) + Low)
End Function
Private Sub btnCreateReport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreateReport.Click
Dim Low As Integer, High As Integer
'~~~ Conversion to Integer
Integer.TryParse(txtLowerBound.Text, Low)
Integer.TryParse(txtUpperBound.Text, High)
'~~~ Call the function, passing LOWER and UPPER bound(range), and display the returned value
txtGenerate.Text = Rand(Low, High).ToString
End Sub
End Class
Assignment is not complete. You need to take care of the rest of the things. :thumb:
Edit:
Changed the datatype from
Decimal to
Integer. (see the below post of jmcilhinney)
As the assignement says recieve lower bound of a range do i need to do that through code or do i need to put some numer in textbox to get the lower bound.
Re: recieve upper bound and lower bound of a range
Quote:
Originally Posted by
sha_shivani
As the assignement says recieve lower bound of a range do i need to do that through code or do i need to put some numer in textbox to get the lower bound.
I think, getting the range through textbox is the best option (ie. what you are doing now). Because, we can test it by giving different values at runtime. But if we hardcode the range, then we need to rewrite the code each time (for providing the new range values). :wave: