derigo
asked on
VB quiz
I am a VB student and I'm 1/2 way through my first semester. I have never had any prior programming experience. If someone could help me with my next quiz, I would greatly appreaciate it.
I learn much better by example.
so here it is:
Test the random # generator by creating 30,000 random integers between 1 and 100 inclusive. Use an array to store the count (frequency) of how many times each # is generated. When a 1 is generated, add 1 to frequency_array(1), when a 2 is generated, add 1 to frequency_array(2), etc. Print the number and the count in 5 columns.
i.e:
1. 323 21. 300 41.275 61. 289 81. 280
2. 288 22. 299 42. 303 62. 295 82. 296
. . . . .
. . . . .
20. 29 40. 99 60. 293 80. 300 100. 30
If anyone could help, I would greatly appreciate it. My quiz is due by 7pm Wed. 3-8-00 EST. Thanks
I learn much better by example.
so here it is:
Test the random # generator by creating 30,000 random integers between 1 and 100 inclusive. Use an array to store the count (frequency) of how many times each # is generated. When a 1 is generated, add 1 to frequency_array(1), when a 2 is generated, add 1 to frequency_array(2), etc. Print the number and the count in 5 columns.
i.e:
1. 323 21. 300 41.275 61. 289 81. 280
2. 288 22. 299 42. 303 62. 295 82. 296
. . . . .
. . . . .
20. 29 40. 99 60. 293 80. 300 100. 30
If anyone could help, I would greatly appreciate it. My quiz is due by 7pm Wed. 3-8-00 EST. Thanks
ASKER
I need help with it all to be honest with you.
ASKER
I know that rnd generates the #s, but how do I make sure they are between 1 -100? how do I search those #s and how do I store those #s in the array? Is it a 1 or 2 dimentional array, and how do I print it to the form in 5 even columns? If you don't think I've offered enough points for this, let me know and I'll adjust accordingly. I just really need the help.
Look up Rnd in Help:
To produce random integers in a given range, use this formula:
Int((upperbound - lowerbound + 1) * Rnd + lowerbound)
To start...
Private Sub Command1_Click()
Dim lRandom As Long
Dim i As Long
For i = 0 To 300 '30000 later...
'Int((upperbound - lowerbound + 1) * Rnd + lowerbound)
'Int((100 - 1 + 1) * Rnd + 1)
'Int(100 * Rnd + 1)
lRandom = Int(100 * Rnd + 1)
Debug.Print lRandom
Next
End Sub
Private Sub Form_Load()
Randomize
End Sub
To produce random integers in a given range, use this formula:
Int((upperbound - lowerbound + 1) * Rnd + lowerbound)
To start...
Private Sub Command1_Click()
Dim lRandom As Long
Dim i As Long
For i = 0 To 300 '30000 later...
'Int((upperbound - lowerbound + 1) * Rnd + lowerbound)
'Int((100 - 1 + 1) * Rnd + 1)
'Int(100 * Rnd + 1)
lRandom = Int(100 * Rnd + 1)
Debug.Print lRandom
Next
End Sub
Private Sub Form_Load()
Randomize
End Sub
Dim intCount(100) As Integer
Dim intIndex As Integer
Dim intTest As Integer
Randomize
For intTest = 1 To 30000
intIndex = CInt((99 * Rnd()) + 1)
intCount(intIndex) = intCount(intIndex) + 1
Next intTest
intTest = 0
For intIndex = 1 To 100
Debug.Print intIndex & " " & intCount(intIndex)
intTest = intTest + intCount(intIndex)
Next intIndex
Debug.Print intTest & " Tests Performed"
Next step, store the results in an array.
Private Sub Command1_Click()
Dim lRandom As Long
'Declare your array to store 100 numbers
Dim freq_array(1 To 100) As Long
Dim i As Long
'Disable the button until we are done
Command1.Enabled = False
For i = 0 To 30000
lRandom = Int(100 * Rnd + 1)
'Add 1 to the array number that corresponds with
'the random number generated.
'This will count how many times each number is generated.
freq_array(lRandom) = freq_array(lRandom) + 1
Next
Command1.Enabled = True
'Check the results:
For i = 1 To 100 'Look at each element of the array
Debug.Print Format(i, "000") & ": " & CStr(freq_array(i)) & " times"
Next
End Sub
Private Sub Command1_Click()
Dim lRandom As Long
'Declare your array to store 100 numbers
Dim freq_array(1 To 100) As Long
Dim i As Long
'Disable the button until we are done
Command1.Enabled = False
For i = 0 To 30000
lRandom = Int(100 * Rnd + 1)
'Add 1 to the array number that corresponds with
'the random number generated.
'This will count how many times each number is generated.
freq_array(lRandom) = freq_array(lRandom) + 1
Next
Command1.Enabled = True
'Check the results:
For i = 1 To 100 'Look at each element of the array
Debug.Print Format(i, "000") & ": " & CStr(freq_array(i)) & " times"
Next
End Sub
ASKER
Eric37,
Thank you for your comment... I'm not sure what you mean by upperbound and lowerbound. would those be my values of 1 - 30,000? If you could actually writ the code for this, it would help greatly and I would give you all the points you want within my power. I know that when I see the actual code, It'll click in my head.
Thank you for your comment... I'm not sure what you mean by upperbound and lowerbound. would those be my values of 1 - 30,000? If you could actually writ the code for this, it would help greatly and I would give you all the points you want within my power. I know that when I see the actual code, It'll click in my head.
Ooops.. just read the 5 column thing.. (its been a looooong time since I have been in school.. LOL).. Now correcting & simplifying.. Change the above to...
<----- Code Begin ----->
Dim intCount(100) As Integer
Dim intIndex As Integer
Dim intTest As Integer
Randomize
For intTest = 1 To 30000
intIndex = CInt((99 * Rnd()) + 1)
intCount(intIndex) = intCount(intIndex) + 1
Next intTest
For intIndex = 1 To 100 Step 5
Debug.Print _
intIndex + 0 & " " & intCount(intIndex + 0) & vbTab; _
intIndex + 1 & " " & intCount(intIndex + 1) & vbTab; _
intIndex + 2 & " " & intCount(intIndex + 2) & vbTab; _
intIndex + 3 & " " & intCount(intIndex + 3) & vbTab; _
intIndex + 4 & " " & intCount(intIndex + 4)
Next intIndex
<----- Code End ----->
<----- Code Begin ----->
Dim intCount(100) As Integer
Dim intIndex As Integer
Dim intTest As Integer
Randomize
For intTest = 1 To 30000
intIndex = CInt((99 * Rnd()) + 1)
intCount(intIndex) = intCount(intIndex) + 1
Next intTest
For intIndex = 1 To 100 Step 5
Debug.Print _
intIndex + 0 & " " & intCount(intIndex + 0) & vbTab; _
intIndex + 1 & " " & intCount(intIndex + 1) & vbTab; _
intIndex + 2 & " " & intCount(intIndex + 2) & vbTab; _
intIndex + 3 & " " & intCount(intIndex + 3) & vbTab; _
intIndex + 4 & " " & intCount(intIndex + 4)
Next intIndex
<----- Code End ----->
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
wsh2,
Thank you so much!!!
I know this souns stupid, but what does "Randomize" under the dim statements do? I mean you're using rnd later and I thought that was what generated the random #s.
also, what does the C in"intIndex = CInt((99 * Rnd()) + 1) " do?
If you don't feel like answering, it's ok. I'm verifying the answer anyway and you'll get the points.
Oh, and how do I get more space between the columns? I'm actually printing this to the form.
Thanks again.
Thank you so much!!!
I know this souns stupid, but what does "Randomize" under the dim statements do? I mean you're using rnd later and I thought that was what generated the random #s.
also, what does the C in"intIndex = CInt((99 * Rnd()) + 1) " do?
If you don't feel like answering, it's ok. I'm verifying the answer anyway and you'll get the points.
Oh, and how do I get more space between the columns? I'm actually printing this to the form.
Thanks again.
ASKER
nevermind about the spacing of the columns, that was a stupid question... I figured that out.
Ok.. to use the Random number function you need to seed the randomizer first. The Randomize command uses the System clock to come up with this number.
The Cint function converts an expression to an INTEGER type. As the RND function returns a Single type (ie.. a number with decimal positions), I am using the Cint to strip all the decimals off. To be honest, the Cint is unneccessary as VB will "type" the number automatically, but I'm not sure which VB compiler you are working with, and I thought I would play it safe by explicitly stating the correct type.
The Cint function converts an expression to an INTEGER type. As the RND function returns a Single type (ie.. a number with decimal positions), I am using the Cint to strip all the decimals off. To be honest, the Cint is unneccessary as VB will "type" the number automatically, but I'm not sure which VB compiler you are working with, and I thought I would play it safe by explicitly stating the correct type.
ASKER
thanks wsh2 I get it now.
Atta guy.. go get em !!! But I want you to know.. I don't come cheap.. you owe me a slice of pizza and a beer.. LOL. Er.. wait.. Sonny?.. Is that YOU?.. Oh My Gawd !!!.. <arms crossed.. foot methodically tapping>.. GET YOUR HOMEWORK DONE !!!.. <sheesh>.
<smile> and a <wink>
<poof>
<smile> and a <wink>
<poof>
Generating the numbers, storing the numbers in the array, or printing the results?