0

I am trying to randomise 5 variables as integers between 1 and 5, but make it so that each variable cannot be equal. I can currently randomise the numbers, but some end up being the same. When I added code that randomises the series of numbers until they are not equal, it just ends up freezing. Any help is appreciated, thanks.

Here is my code:

    Dim rndnum As Random
    Dim qcombo As Boolean = False
    Dim q1 As Integer
    Dim q2 As Integer
    Dim q3 As Integer
    Dim q4 As Integer
    Dim q5 As Integer
    rndnum = New Random

    Do
        q1 = rndnum.Next(1, 5)
        q2 = rndnum.Next(1, 5)
        q3 = rndnum.Next(1, 5)
        q4 = rndnum.Next(1, 5)
        q5 = rndnum.Next(1, 5)
   If q1 = q2 Or q1 = q3 Or q1 = q4 Or q1 = q5 Or q2 = q3 Or q2 = q4 Or q2 = q5 Or q3 = q4 Or q3 = q5 Or q4 = q5 Then
        qcombo = False
    Else
        qcombo = True
    Loop Until qcombo = True
Niam
  • 37
  • 6
  • That just generates 5 unique numbers, doesn't actually save them as 5 separate variables. – Niam Mar 28 '17 at 16:44
  • @Niam If you don't know what to do with that answer to get it in to different variables, then you have some other problems to solve before trying to tackle this one. – Bradley Uffner Mar 28 '17 at 16:48
  • 1
    Read the documentation for the Random.Next method you are using. The possible values it generates are {1,2,3,4}. You are generating five values, hence one of them must be a duplicate. – TnTinMn Mar 28 '17 at 16:48
  • @BradleyUffner What do you mean other problems? Comment to help or something, not to give some vague comment. – Niam Mar 28 '17 at 16:51
  • [The documentation](https://msdn.microsoft.com/en-us/library/2dx6wyd4%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396) that was mentioned. Pay special attention to where it says than `maxValue` is exclusive, meaning it wont be part of the set that could be returned. – Bradley Uffner Mar 28 '17 at 16:51
  • @TnTinMn changing it to (1,6) worked, thanks for the help. – Niam Mar 28 '17 at 16:53
  • @BradleyUffner Thanks for the help – Niam Mar 28 '17 at 16:54
  • In the example, RememberSet contains 5 numbers as Collection variables. You can use it as array using RemeberSet.asArray() and access by index – F.Igor Mar 28 '17 at 16:56
  • One thing to keep in mind with the code you provided (even with max value set to 6) is that it is *theoretically possible* for it to loop forever, never finding unique numbers. In practice, that won't happen with a the small set you are working with, but it could fail, or perform poorly when working with very large sets of numbers. – Bradley Uffner Mar 28 '17 at 16:57
  • The way you would fix that is to place your numbers in to a list, then randomly draw from that list, add it to another list, removing the number from the original list each time. – Bradley Uffner Mar 28 '17 at 16:58
  • I don't plan on using a large set, so I'm fine with the current solution, no time to make it perfect. – Niam Mar 28 '17 at 17:04

1 Answers1

3

One easy way to do it is to build the array and then shuffle it. Please excuse any syntax errors, as my VB is pretty rusty. The basic idea is:

dim numbers(5) as integer
for j = 1 to 5
    numbers(j) = j
next j

' now the shuffle
dim rnd as new Random
for i as integer = 5 to 1 step -1
    dim j as integer = rnd.Next(1, i+1)
    dim temp as integer = numbers(i)
    numbers(i) = numbers(j)
    numbers(j) = temp
next

q1 = numbers(1)
q2 = numbers(2)
q3 = numbers(3)
q4 = numbers(4)
q5 = numbers(5)
Jim Mischel
  • 131,090
  • 20
  • 188
  • 351
  • Just implemented my way throughout the program, your way seems more efficient but i'm tight on time at the moment. Thanks anyway. – Niam Mar 28 '17 at 17:00