-2

I have to write a loop that would produce to make 4 unique numbers. So it would be like 4- 2 - 5 - 7 and it can't be like 4 - 4 - 5- 7 because the 4's aren't unique. It's just I struggle with loop I tried learning loop on YouTube no idea still maybe if I see someone do it in this concept I'll get the hang of if

  • See my answer here (vba) http://stackoverflow.com/questions/34862663/how-can-i-check-in-visual-basic-if-string-has-duplicate-symbols/34866579#34866579 – Jules Jan 25 '16 at 02:26
  • That code didn't help at all – Joe Watson Jan 25 '16 at 02:33
  • Your question has vba tag, that answer is in vba format. You can modify the logic to work in vb.net. – Jules Jan 25 '16 at 02:38
  • This site is not intended for us to just give you code. It is for us to help you correct code that isn't working. Write the code the way you think it should be and, if it doesn't work, post that code here and explain what you think it should be doing. – jmcilhinney Jan 25 '16 at 03:01
  • Of course, to have a hope of writing working code you have to know what it's supposed to do. Do you? I'm not talking about in a vague general sense. I'm talking about the step-by-step specifics. You should be able to explain EXACTLY what steps a person would have to follow to perform this task manually. If you can't then you don't even know what the task entails. Furthermore, that's not even a programming problem. You should have a list of steps written down that your code needs to implement. If you don't have that then you really haven't tried, despite what you might think. – jmcilhinney Jan 25 '16 at 03:02

2 Answers2

2

There's a very easy way to without resorting to loops.

Try this code:

Dim rnd As System.Random = New System.Random()
Dim numbers As Integer() = _
    Enumerable.Range(1, 10).OrderBy(Function (n) rnd.Next()).Take(4).ToArray()

I get this kind of result with this code:

8, 3, 2, 6

You can change the .Range(1, 10) to select any range of numbers you like and the .Take(4) to only take as many numbers as you like.


If you want to do this with a loop, then here's a fairly efficient way:

Dim rnd As System.Random = New System.Random()
Dim pallet As New List(Of Integer)
For i = 1 To 10
    pallet.Add(i)
Next
Dim selected = New List(Of Integer)()
For i = 0 To 3
    Dim j = rnd.Next(0, pallet.Count)
    selected.Add(pallet(j))
    pallet.RemoveAt(j)
Next
Enigmativity
  • 113,464
  • 11
  • 89
  • 172
1

I can only assume that you need 4 unique, random numbers. With this being the assumption, we would first need a method to generate random numbers (borrowed from https://stackoverflow.com/a/2677819/3579199):

Public Function GetRandom(ByVal Min As Integer, ByVal Max As Integer) As Integer
   Static Generator As System.Random = New System.Random()
   Return Generator.Next(Min, Max)
End Function

This method will return a random number that is >= Min and < Max. We can now use this method to generate 4, unique random numbers in a loop. For the following example, I'll assume you need 4 numbers between 1 and 99, inclusive:

Dim count = 0
Dim numbers(3) As Integer
Do
    Dim randomNumber = GetRandom(1, 100)
    If(Array.Exists(numbers, Function(element)
                                  Return element.Equals(randomNumber)
                              End Function))
        Continue Do 
    End If

   numbers(count) = randomNumber
   count += 1
Loop Until count = 4
Community
  • 1
  • 1
Patrick
  • 185
  • 1
  • 11