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. Help me tune up this class, pls

Help me tune up this class, pls

Scheduled Pinned Locked Moved Visual Basic
helplounge
2 Posts 2 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.
  • K Offline
    K Offline
    king gamo
    wrote on last edited by
    #1

    i have this class below which i'm using to uniquely generate a set of numbers. What it does is if i want 10 numbers randomly selected out of 11 i will pass it as a parameter to it and it'll return me the unique numbers. i dont want repetitions of the same number and it does what i want exactly... the problem is that atimes it gets into an infinite loop especially when i want 4 out of 5 or 3 out of 4. the class Public Class UniqueNumbers Dim randCtr As New Random Public Function genNumbers(ByVal totalNeeded As Integer, ByVal AvailableRange As Integer) As Integer() Dim var1, var3 As Integer Dim generatedNumbers(totalNeeded - 1) As Integer Dim returnedNumbers(totalNeeded - 1) As Integer For var1 = 0 To (totalNeeded - 1) generatedNumbers(var1) = randCtr.Next(1, (AvailableRange + 1)) returnedNumbers(var1) = generatedNumbers(var1) naa: For var3 = 0 To var1 If returnedNumbers(var3) = generatedNumbers(var1) Then generatedNumbers(var1) = randCtr.Next(1, (AvailableRange + 1)) If var1 <> 0 Then 'varar(var1) = vararr(var1) GoTo naa End If End If Next returnedNumbers(var1) = generatedNumbers(var1) Next genNumbers = returnedNumbers Return genNumbers End Function End Class all that i mean is if i want 3 out of 4 it can return 123 or 321 or 231 anything other than repeating one number twice like 112 or 122 or 332. My main problem is i want it to be faster and atimes it gets into a hang or infinite loop. Help me out guys. Love makes the world go round

    G 1 Reply Last reply
    0
    • K king gamo

      i have this class below which i'm using to uniquely generate a set of numbers. What it does is if i want 10 numbers randomly selected out of 11 i will pass it as a parameter to it and it'll return me the unique numbers. i dont want repetitions of the same number and it does what i want exactly... the problem is that atimes it gets into an infinite loop especially when i want 4 out of 5 or 3 out of 4. the class Public Class UniqueNumbers Dim randCtr As New Random Public Function genNumbers(ByVal totalNeeded As Integer, ByVal AvailableRange As Integer) As Integer() Dim var1, var3 As Integer Dim generatedNumbers(totalNeeded - 1) As Integer Dim returnedNumbers(totalNeeded - 1) As Integer For var1 = 0 To (totalNeeded - 1) generatedNumbers(var1) = randCtr.Next(1, (AvailableRange + 1)) returnedNumbers(var1) = generatedNumbers(var1) naa: For var3 = 0 To var1 If returnedNumbers(var3) = generatedNumbers(var1) Then generatedNumbers(var1) = randCtr.Next(1, (AvailableRange + 1)) If var1 <> 0 Then 'varar(var1) = vararr(var1) GoTo naa End If End If Next returnedNumbers(var1) = generatedNumbers(var1) Next genNumbers = returnedNumbers Return genNumbers End Function End Class all that i mean is if i want 3 out of 4 it can return 123 or 321 or 231 anything other than repeating one number twice like 112 or 122 or 332. My main problem is i want it to be faster and atimes it gets into a hang or infinite loop. Help me out guys. Love makes the world go round

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      I can very well imagine that your code goes into an eternal loop, it's rather buggy. For example, the first random number created for each position in the array will always be replaced by a new random number, as you regard it as a duplicate as it is equal to itself. Also, if a number is the same as the first in the array, you pick a new number and accept it without checking if the new number is the same as the first in the array. Perhaps not the most efficient, but very simple and straight forward: Put all the possible numbers in a list. Pick one of them by random and remove it from the list. Repeat until you have the desired number of numbers.

      --- b { font-weight: normal; }

      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