Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Finding Duplicate numbers

Finding Duplicate numbers

Scheduled Pinned Locked Moved Visual Basic
helpdata-structuresquestionlounge
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    CowlitzTroy
    wrote on last edited by
    #1

    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 generator

    Dim vRoll() As Integer 'User entered variable for the array
    ReDim vRoll(nDice) 'dynamic number entered by the user

    Dim cntDice As Integer = 0

    Dim myArray As New List(Of Integer)
    Dim temp As Integer

    myArray.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
    Next

    So if anyone is willing to give me advice I would greatly appreciate it. Thanks!

    Troy E Bouchard

    D M 2 Replies Last reply
    0
    • C CowlitzTroy

      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 generator

      Dim vRoll() As Integer 'User entered variable for the array
      ReDim vRoll(nDice) 'dynamic number entered by the user

      Dim cntDice As Integer = 0

      Dim myArray As New List(Of Integer)
      Dim temp As Integer

      myArray.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
      Next

      So if anyone is willing to give me advice I would greatly appreciate it. Thanks!

      Troy E Bouchard

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • C CowlitzTroy

        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 generator

        Dim vRoll() As Integer 'User entered variable for the array
        ReDim vRoll(nDice) 'dynamic number entered by the user

        Dim cntDice As Integer = 0

        Dim myArray As New List(Of Integer)
        Dim temp As Integer

        myArray.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
        Next

        So if anyone is willing to give me advice I would greatly appreciate it. Thanks!

        Troy E Bouchard

        M Offline
        M Offline
        MicroVirus
        wrote on last edited by
        #3

        You could also sort the list first, then duplicates would show up next to each other.

        D 1 Reply Last reply
        0
        • M MicroVirus

          You could also sort the list first, then duplicates would show up next to each other.

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups