0

I am trying to create an array of numbers that are not repeated. The order of priority is from top to bottom, left to right, where I'm trying to get a range of x to y. But need dont be repeat values in ranges

  Dim arrayCarton(9, 3) As Integer

    Private Sub generar_carton()
        Randomize()
        filas = 3
        columnas = 9
        Dim RandomNumber As Integer
        For j = 0 To columnas - 1
            For i = 0 To filas - 1
                Dim label As Label = CType(Panel2.Controls("Carton" & i & j), Label)
                If j = 0 Then
                    RandomNumber = CInt(Math.Floor(((9 * (j + 1)) - 1) * Rnd())) + 1                   
                End If
                If j < 8 And j > 0 Then
                    RandomNumber = CInt(Math.Floor(((j * 10 + 9) - j * 10) * Rnd())) + j * 10

                End If
                If j = 8 Then
                    RandomNumber = CInt(Math.Floor(((9 * 10) - j * 10) * Rnd())) + j * 10 + 1
                End If
                arrayCarton(j, i) = RandomNumber
                label.Text = arrayCarton(j, i)
            Next
        Next
    End Sub

One can see that some numbers are repeated, output:

enter image description here

Cazs
  • 85
  • 7
  • 1
    Random doesn't mean non-repeating...why would you expect to never have non-repeating? If that's what you want - then you'll have to check that you haven't used that number before. – Mark Brackett Feb 26 '16 at 18:46
  • I'm trying to evaluate the random number has not been repeated, but I can not how to do it – Cazs Feb 26 '16 at 18:48
  • it can be seen that the numbers 10 and 23 are repeated there is the problem that I do not want it to be repeated – Cazs Feb 26 '16 at 18:50
  • what is that range of values and how many? – Ňɏssa Pøngjǣrdenlarp Feb 26 '16 at 18:51
  • 1 - 90 but these only need 27 but they have to be ordered from top to bottom and left to right – Cazs Feb 26 '16 at 18:52
  • 1
    you just need a variation on the answer in the one link: `Enumerable.Range(1, 90).OrderBy(Function(r) RNG.Next).Take(27).OrderBy(Function(x) x).ToArray()` Use the NET `Random` for the RNG, not that old VB `Rnd` function – Ňɏssa Pøngjǣrdenlarp Feb 26 '16 at 18:56
  • Create a list of numbers (RandomNumbersList) and each time you produce a random, search in the list if exists. If exists loop to next random number. If not exist, add it to the list and then use it as you like. I can't think another solution. – shadow Feb 26 '16 at 18:58
  • RNG.Next is for vb.net 2010? – Cazs Feb 26 '16 at 19:41
  • 1
    If more that one person comments, you need to ping them using @UserName else we see nothing (you get pinged for all of them because it is your post). RNG would be `Private RNG = New Random()` - its an easier to use version than the VB function. Make ***one** for the entire app/form – Ňɏssa Pøngjǣrdenlarp Feb 26 '16 at 19:46
  • @Plutonix, thanks them how declare array? Dim arrayCarton(9, 3) As Integer this not be can give one example array used for enumerable etc..? – Cazs Feb 26 '16 at 20:09
  • 1
    Wait...did you assign the result to something or are you trying to use that comment as is? That was intended for use with the [duoe link](http://stackoverflow.com/a/35120518/1070452) which explains some concepts...`picks = Enumerable.Range...` – Ňɏssa Pøngjǣrdenlarp Feb 26 '16 at 20:28
  • @Plutonix thanks for help – Cazs Feb 26 '16 at 21:53
  • @Plutonix i don' t understant why Enumerable.Range(80, 90) have random ouput than more 90 numbers – Cazs Feb 26 '16 at 22:00
  • 1
    It should have exactly 90 - that says "give me a sequence of 90 integers starting with 80". They are not random but put in a random order if you `OrderBy` Please take a second to upvote [the linked answer which explained how to do that](http://stackoverflow.com/a/35120518/1070452). Upvotes help people find good answers – Ňɏssa Pøngjǣrdenlarp Feb 26 '16 at 22:06
  • ohhh wait, now understant 80 is number to start and 90 is cuantity the numbers produce – Cazs Feb 26 '16 at 22:09

0 Answers0