# QuestionRandom Binary Matrix

#### tariq75

##### New member
Hello Everyone. I would like to create a random binary matrix ( only 0 and 1) of any size of row and column in vb.net. I am not getting any suitable code for that. Any help will be highly appreciable.
Regards.
Tariq

#### tariq75

##### New member
In MATLAB "randi([0 1], 5,5)" command generates 5 x 5 binary matrix. But i am not finding any suitable code for creating random binary matrix in vb.net

#### jmcilhinney

##### VB.NET Forum Moderator
Staff member
There is no single line of code for that specifically. It's no surprise that Matlab has dedicated maths functionality that VB.NET doesn't, given that Matlab is a dedicated maths application. If you want a 2D array in VB.NET then create a 2D array. It's easy to find information on doing that. If you want to set each element then do that. It's easy to find information on traversing a 2D array and information on setting elements in an array. If you want to set each element using a random value in a specific range then do that. It's easy to find information on generating random values. You're not finding the information you want because you're thinking of it as a single operation, which it is not in VB.NET. It's basically four operations - create array, traverse array, generate random number, set array element - and you can research each one individually.

#### tariq75

##### New member
Thank jmcilhinney for your advice. ... I found the solution...

Private mRand As New Random
Private rndMatrix As Integer(,)

Public Sub BuildMatrix(rows As Integer, columns As Integer)
ReDim rndMatrix(rows, columns)
For x = 0 To rows
Dim valu = mRand.Next(0, Integer.MaxValue)
For y = 0 To columns
rndMatrix(x, y) = ((valu >> y) And &H1)
Console.Write(String.Format("{0} ", rndMatrix(x, y)))
Next
Console.Write(Environment.NewLine)
Next
End Sub

Replies
3
Views
886
Replies
1
Views
707
Replies
0
Views
295
Replies
1
Views
660
Replies
0
Views
3K