# Visual Basic Lottery number generator in 2d array

• ### Question

• Hello,

I'm building a Lottery number generator with Visual Basic (XAML). I want a random number generator with only unique numbers. The random numbers have to be stored in a Text Box (in my case I called it LotteryCard).

I have the next code:

```Dim Lottery(A, B) As Integer
Dim Random As New System.Random()
Dim C, R As Integer
Dim LotteryNumbers As String

For C = 0 To 2
For R = 0 To 2
Lottery(C, R) = Random.Next(0, 100)
LotteryNumbers += Lottery(C, R) & " , "
Next R
LotteryCard.text = LotteryNumbers
Next C```

When I run my code I only see a line with numbers in my LotteryCard.text Textbox with duplicates like:

22, 33, 22, 33, 44, 55, 60, 6, 4, 2, 3, 4, 5 etc.

I have three questions:

1. Is there a way to generate unique random numbers?

2. Is there a way to order my numbers in a kind of range (first row: only 0 to 9, second row 10 to 19, third row 20 to 29).

3. Is there a way show my numbers in the Textbox with 3 rows and 3 columns like:

1 6 7 2

12 15 18 19

20 23 27 29

Jonathan

Saturday, February 8, 2020 6:47 PM

• Hi

OK, somewhat more understandable. Just remember that the reader needs all the CORRECT information and can't guess what you mean.

*

Here is an amended version, none of your original code remains, but if you are determined, you can maybe adapt to suit.

```' Form1 with Multiline TextBox1
' and Button1
Option Strict On
Option Explicit On
Public Class Form1
Dim Lottery(2, 2) As Integer
Dim Random As New Random()
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
TextBox1.Clear()
Dim lst As New List(Of Integer)
Dim rn As Integer = Nothing
Dim st As Integer = 10
Dim en As Integer = 20
For R As Integer = 0 To 2
Dim C As Integer = 0
While C < 3
rn = Random.Next(st, en)
If Not lst.Contains(rn) Then
Lottery(R, C) = rn
C += 1
End If
End While
st += 10
en += 10
Next
lst.Sort()
For i As Integer = 0 To lst.Count - 1 Step 3
Next
End Sub
End Class```

Regards Les, Livingston, Scotland

• Edited by Sunday, March 1, 2020 3:12 PM
• Marked as answer by Sunday, March 1, 2020 3:20 PM
Sunday, March 1, 2020 3:11 PM

### All replies

• a) You can order a range by a random number

c)  Make sure the textbox is multiline

```        Dim LotteryNumbers As String = ""
Dim x As Int16 = 0

Dim RandomNumberList = Enumerable.Range(0, 100).OrderBy(Function(n) Random.Next(0, 100)).ToList()
For C = 0 To 2
For R = 0 To 2
Lottery(C, R) = RandomNumberList(x)
x += 1
LotteryNumbers &= Lottery(C, R) & "  "
Next R
LotteryNumbers &= Environment.NewLine
Next C LotteryCard.text = LotteryNumbers ```

• Marked as answer by Wednesday, February 19, 2020 6:26 PM
• Unmarked as answer by Wednesday, February 19, 2020 6:47 PM
Sunday, February 9, 2020 12:37 AM
• I optimized my code:

```Dim Lottery(A, B) As Integer
Dim Random As New System.Random()
Dim C, R As Integer
Dim F As Integer = 0
Dim G As Integer = 10
Dim LotteryNumbers As String

For C = 0 To 2
F = F + 10
G = G + 10
For R = 0 To 2
Lottery(C, R) = Random.Next(F, G)
LotteryNumbers += Lottery(C, R) & " , "
Next R
LotteryNumbers &= vbCrlf
LotteryCard.text = LotteryNumbers
Next C```

Row 1: 10 to 20

Row 2: 20 to 30

Row 3: 30 to 40

I still figure out how to delete the duplicates in my code. There have to be a code way to do it?

• Edited by Monday, February 10, 2020 8:50 AM
Monday, February 10, 2020 8:49 AM
• ```Dim Lottery(A, B) As Integer
Dim Random As New System.Random()
Dim C, R As Integer
Dim F As Integer = 0
Dim G As Integer = 10
Dim x as Integer
Dim LotteryList as New List (of Integer)=
Dim LotteryNumbers As String

For C = 0 To 2
F = F + 10
G = G + 10
For R = 0 To 2
x = Random.Next(F, G)
If LotteryList.contains(x) is False Then
Else
LotteryList.remove(x)
Next R
LotteryCard.text = LotteryCard.text & LotteryNumbers.ToString & VbCrlf
Next C```

I still struggling with the code. I think about creating a list with random numbers and if a number is already in the list the number will be deleted. I have read on the internet that many programmers recommend it, but I get a error when I use the List method:

System.Collections.Generic.List`1[System.Int32]

What I'm doing wrong?

• Edited by Sunday, February 16, 2020 1:57 PM
Sunday, February 16, 2020 1:51 PM
• Hello,

This may not be exactly what you are asking for but should if not be a good direction.

Extension method to chunk the list of integers done below

```Public Module ListExtensions
<Runtime.CompilerServices.Extension>
Public Function ChunkBy(Of T)(source As List(Of T), chunkSize As Integer) As List(Of List(Of T))
Return source.Select(
Function(x, i)
Return New With {Key .Index = i, Key .Value = x}
End Function).
GroupBy(Function(x) x.Index \ chunkSize).
Select(Function(x) x.Select(Function(v) v.Value).ToList()).
ToList()
End Function
End Module
```

The following is done in a Windows Form so you could treat it the same or work through the DataContext/data binding. Change the ChunkBy to meet your needs.

```Dim sb As New StringBuilder
Dim random As New Random
Dim randomNumberList = Enumerable.Range(1, 40).OrderBy(Function(n) random.Next(0, 100)).ToList()
Dim results = randomNumberList.ChunkBy(7)

Dim result1 = randomNumberList.Where(Function(item) item >= 1 AndAlso item <= 10).ToArray()
Dim result2 = randomNumberList.Where(Function(item) item >= 10 AndAlso item <= 19).ToArray()
Dim result3 = randomNumberList.Where(Function(item) item >= 20 AndAlso item <= 29).ToArray()

sb.AppendLine(String.Join(" ", result1))
sb.AppendLine(String.Join(" ", result2))
sb.AppendLine(String.Join(" ", result3))
TextBox1.Text = sb.ToString()```

Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

NuGet BaseConnectionLibrary for database connections.

StackOverFlow

• Marked as answer by Wednesday, February 19, 2020 6:05 PM
• Unmarked as answer by Wednesday, February 19, 2020 6:46 PM
Sunday, February 16, 2020 3:38 PM
• Thank you very much for the reply and the given code.

I never saw this type of code in my instruction books. I want to know if it is possible with my code in my message from February 16 2020 to generate unique numbers.

This must be possible? I want to understand want I'm doing wrong and how to fix it in my already written code

I want to make a 2d array with 3 rows and 3 columns with unique numbers. When I run my code I get the numbers in the TextBox, but with duplicates:

19   18   13
22   25   23
33   34   33

Sorry for 'dumb' questions, I'm a beginning hobby programmer.

• Edited by Tuesday, February 18, 2020 7:24 PM
Tuesday, February 18, 2020 7:17 PM
• In regards to never seeing code like this in a book, not surprised, it more advance level. You should try it (and that is how to learn quickly rather than going old school)

Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

NuGet BaseConnectionLibrary for database connections.

StackOverFlow

• Marked as answer by Wednesday, February 19, 2020 6:05 PM
• Unmarked as answer by Wednesday, February 19, 2020 6:46 PM
Tuesday, February 18, 2020 7:36 PM
• Thank you very much for the reply and the given code.

I never saw this type of code in my instruction books. I want to know if it is possible with my code in my message from February 16 2020 to generate unique numbers.

This must be possible? I want to understand want I'm doing wrong and how to fix it in my already written code

I want to make a 2d array with 3 rows and 3 columns with unique numbers. When I run my code I get the numbers in the TextBox, but with duplicates:

19   18   13
22   25   23
33   34   33

Sorry for 'dumb' questions, I'm a beginning hobby programmer.

Hi

Here is one way. Using as much of your code as I could.

NOTE: I changed the numbers to be 1 99 inclusive - iff you need a different range then adjust as needed. I put the code into a Button Click event handler so that the numbers can be repeatedly generated.

This is a stand alone example. If you want to try it, start a new project and put a TextBox1 and a Button1 onto Form1 in the Designer, then copy/replace all of the default Form1 code with this code.

```' Form1 with Multiline TextBox1
' and Button1
Option Strict On
Option Explicit On
Public Class Form1
Dim Lottery(2, 2) As Integer
Dim Random As New Random()
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
TextBox1.Clear()
' get 9 unique numbers
Dim lst As New List(Of Integer)
Dim rn As Integer = Nothing
For R As Integer = 0 To 8
While lst.Count < 9

' changed to 1 - lottery
' numbers don't use 0
rn = Random.Next(1, 100)
End While
Next

' fill array from lst
' and put in textbox1
Dim count As Integer = 0
For C As Integer = 0 To 2
For R As Integer = 0 To 2
Lottery(C, R) = lst(count)
count += 1
If count Mod 3 = 0 Then
TextBox1.AppendText(vbCrLf)
End If
Next
Next
End Sub
End Class```

Regards Les, Livingston, Scotland

• Marked as answer by Wednesday, February 19, 2020 6:05 PM
• Unmarked as answer by Wednesday, February 19, 2020 6:46 PM
Tuesday, February 18, 2020 10:18 PM
• Thank you all for the help. I now have unique random numbers in a XAML array instead of a Form. I made some changes in my code and it works.

But I have one further question:

Is it also possible to have a kind of order in the rows?

For example:

First row: min number is 1 and max is 10

Second row: min number is 10 and max is 20.

Third row: min number is 20 and max is 30.

Etc.

• Edited by Wednesday, February 19, 2020 6:53 PM
Wednesday, February 19, 2020 6:05 PM
• Still struggling with the code.

I want to following in my program:

First row: min number is 1 and max is 10

Second row: min number is 10 and max is 20.

Third row: min number is 20 and max is 30.

All unique numbers. My code still generate the same numbers in a row. I want to make my code like the way leshay mentioned in his post.

Thanks in advance and sorry for the many questions.

Sunday, March 1, 2020 1:06 PM
• Hi

You must think more carefully.

The number 10. Which row? You say 1-10 in first row so it must go there!

You also say second row min is 10 so it must go there! Which one?

BTW: another point of confusion to sort out. You are using 9 numbers between 1 and ??? inclusive - please fill in the ??? It seems you want to generate 100 random numbers, but do you then want to discard any over 30 (third row max)?

Regards Les, Livingston, Scotland

• Edited by Sunday, March 1, 2020 1:58 PM
Sunday, March 1, 2020 1:52 PM
• Thanks for the response.

I want to have the numbers in the way (for example):

10, 13, 17

20, 24, 27

34, 36, 39

I want three rows and in every row three unique numbers. Total of 9 numbers.

I'm really stuck in the code. This code is working, but there are still duplicates:

```Dim Lottery(A, B) As Integer
Dim Random As New System.Random()
Dim C, R As Integer
Dim F As Integer = 0
Dim G As Integer = 10
Dim LotteryNumbers As String

For C = 0 To 2
F = F + 10
G = G + 10
For R = 0 To 2
Lottery(C, R) = Random.Next(F, G)
LotteryNumbers += Lottery(C, R) & " , "
Next R
LotteryNumbers &= vbCrlf
LotteryCard.text = LotteryNumbers
Next C```
Maybe I have to insert a loop?

Sunday, March 1, 2020 2:00 PM
• Hi

Unless you read and understand and clarify the points I raised, I can not assist further.

1. Why do you seem to want to generate 100 random numbers.

2. Whar EXACTLY is the lowest and highest possible number (INCLUSIVE)?

3. Clarify EXACTLY te range of numbers for each row (INCLUSIVE)

Regards Les, Livingston, Scotland

• Edited by Sunday, March 1, 2020 2:23 PM
Sunday, March 1, 2020 2:23 PM
• Sorry if my messages are confusing.

1. I don't want to generate 100 random numbers anymore. It was a fault in the beginning of writing my code. That's why I write this code:

```     For C = 0 To 2
F = F + 10
G = G + 10
For R = 0 To 2
Lottery(C, R) = Random.Next(F, G)
LotteryNumbers += Lottery(C, R) & " , "```

The range of the random number generator is different in every row:

Row 1: 10 (F + 10) , 20 (G + 10)

Row 2: 20 (F + 10) , 30 (G + 10).

2. The lowest number is 10 and the highest number is 39 in the array.

3. Range in each row:

1. 10 till 19 (19 is the max)

2. 20 till 29 (29 is the max)

3. 30 till 39 (39 is the max).

Sunday, March 1, 2020 2:31 PM
• Hi

OK, somewhat more understandable. Just remember that the reader needs all the CORRECT information and can't guess what you mean.

*

Here is an amended version, none of your original code remains, but if you are determined, you can maybe adapt to suit.

```' Form1 with Multiline TextBox1
' and Button1
Option Strict On
Option Explicit On
Public Class Form1
Dim Lottery(2, 2) As Integer
Dim Random As New Random()
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
TextBox1.Clear()
Dim lst As New List(Of Integer)
Dim rn As Integer = Nothing
Dim st As Integer = 10
Dim en As Integer = 20
For R As Integer = 0 To 2
Dim C As Integer = 0
While C < 3
rn = Random.Next(st, en)
If Not lst.Contains(rn) Then
Lottery(R, C) = rn
C += 1
End If
End While
st += 10
en += 10
Next
lst.Sort()
For i As Integer = 0 To lst.Count - 1 Step 3