Finding Duplicate numbers
-
My class is now over. I had an issue with the the assignment. I couldn't get the program to find and display the duplicate random numbers. I've looked for almost two weeks for an answer and I'm hoping that someone here can help me. This was the assignment: Present the following report – (you will modify this based on user input and the actual rolls. Number of dice:2 Number of sides:6 Number of rolls:2 Dice 1 Roll 1 = 5 Dice 2 Roll 1 = 6 Dice 1 Roll 2 = 1 Dice 2 Roll 2 = 1 Number of rolls where all dice were the same: 1 I got everything but the number of rolls where all the dice were the same. Can someone please advise me as to how this should have been done? Here is a snippet of what I did:
'Start the randomization process
Dim RandomClass As New Random() 'Our random number generatorDim vRoll() As Integer 'User entered variable for the array
ReDim vRoll(nDice) 'dynamic number entered by the userDim cntDice As Integer = 0
Dim myArray As New List(Of Integer)
Dim temp As IntegermyArray.Clear()
'Get the number of rolls and the number of dice and then randomize the rolls.
For i As Integer = 1 To rRoll
For j As Integer = 1 To nDice
temp = RandomClass.Next(1, dSize + 1)
LST_Items.Items.Add("Dice " & j & " Roll " & i & " = " & temp.ToString)
If Not myArray.Contains(temp) Then
cntDice += 1
LBL_Same.Text = cntDice
Else
LBL_Same.Text = "0"
End If
Next
NextSo if anyone is willing to give me advice I would greatly appreciate it. Thanks!
Troy E Bouchard
-
My class is now over. I had an issue with the the assignment. I couldn't get the program to find and display the duplicate random numbers. I've looked for almost two weeks for an answer and I'm hoping that someone here can help me. This was the assignment: Present the following report – (you will modify this based on user input and the actual rolls. Number of dice:2 Number of sides:6 Number of rolls:2 Dice 1 Roll 1 = 5 Dice 2 Roll 1 = 6 Dice 1 Roll 2 = 1 Dice 2 Roll 2 = 1 Number of rolls where all dice were the same: 1 I got everything but the number of rolls where all the dice were the same. Can someone please advise me as to how this should have been done? Here is a snippet of what I did:
'Start the randomization process
Dim RandomClass As New Random() 'Our random number generatorDim vRoll() As Integer 'User entered variable for the array
ReDim vRoll(nDice) 'dynamic number entered by the userDim cntDice As Integer = 0
Dim myArray As New List(Of Integer)
Dim temp As IntegermyArray.Clear()
'Get the number of rolls and the number of dice and then randomize the rolls.
For i As Integer = 1 To rRoll
For j As Integer = 1 To nDice
temp = RandomClass.Next(1, dSize + 1)
LST_Items.Items.Add("Dice " & j & " Roll " & i & " = " & temp.ToString)
If Not myArray.Contains(temp) Then
cntDice += 1
LBL_Same.Text = cntDice
Else
LBL_Same.Text = "0"
End If
Next
NextSo if anyone is willing to give me advice I would greatly appreciate it. Thanks!
Troy E Bouchard
First, you don't need that List. You're not trying to remember every single die roll. Second, you never compare the roll of the first die to the roll of the second. Once you do that, you simply increment a counter that tracks how many times both dice are equal. Third, you don't need two for loops, just one.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
My class is now over. I had an issue with the the assignment. I couldn't get the program to find and display the duplicate random numbers. I've looked for almost two weeks for an answer and I'm hoping that someone here can help me. This was the assignment: Present the following report – (you will modify this based on user input and the actual rolls. Number of dice:2 Number of sides:6 Number of rolls:2 Dice 1 Roll 1 = 5 Dice 2 Roll 1 = 6 Dice 1 Roll 2 = 1 Dice 2 Roll 2 = 1 Number of rolls where all dice were the same: 1 I got everything but the number of rolls where all the dice were the same. Can someone please advise me as to how this should have been done? Here is a snippet of what I did:
'Start the randomization process
Dim RandomClass As New Random() 'Our random number generatorDim vRoll() As Integer 'User entered variable for the array
ReDim vRoll(nDice) 'dynamic number entered by the userDim cntDice As Integer = 0
Dim myArray As New List(Of Integer)
Dim temp As IntegermyArray.Clear()
'Get the number of rolls and the number of dice and then randomize the rolls.
For i As Integer = 1 To rRoll
For j As Integer = 1 To nDice
temp = RandomClass.Next(1, dSize + 1)
LST_Items.Items.Add("Dice " & j & " Roll " & i & " = " & temp.ToString)
If Not myArray.Contains(temp) Then
cntDice += 1
LBL_Same.Text = cntDice
Else
LBL_Same.Text = "0"
End If
Next
NextSo if anyone is willing to give me advice I would greatly appreciate it. Thanks!
Troy E Bouchard
You could also sort the list first, then duplicates would show up next to each other.
-
You could also sort the list first, then duplicates would show up next to each other.
Don't need the list at all. Gen the first number on the roll and remember it. For each subsequent die, check it's value against the first die. Finding a non-dupliate is very easy. If you get through the loop withouting finding a non-duplicate, well, you know.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak