none
Random Binary Matrix RRS feed

  • Question

  • 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
    Wednesday, May 1, 2019 6:31 PM

All replies

  • 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
    Wednesday, May 1, 2019 6:50 PM
  • Hi Tariq,
    try this demo code:

          Dim arr(10, 10) As Boolean
    
          Dim rnd As New Random
    
          For i = arr.GetLowerBound(0) To arr.GetUpperBound(0)
            For k = arr.GetLowerBound(1) To arr.GetUpperBound(1)
              arr(i, k) = rnd.NextDouble < 0.5
            Next
          Next


    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks

    Wednesday, May 1, 2019 6:52 PM
  • Hi

    Another way:

    ' Form1 with Label1
    Option Strict On
    Option Explicit On
    Public Class Form1
        Dim rand As New Random
        Dim matrix(0, 0) As String
        Dim lb As New ListBox
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            ' create a random size array
            ReDim matrix(rand.Next(4, 10), rand.Next(4, 10))
    
            ' add columns
            For i As Integer = 0 To matrix.GetUpperBound(0)
                For j As Integer = 0 To matrix.GetUpperBound(1)
                    matrix(i, j) = rand.Next(0, 2).ToString
                Next
            Next
    
            Label1.Text = "Matrix of " & (matrix.GetUpperBound(0) + 1).ToString & " x " & (matrix.GetUpperBound(1) + 1).ToString
    
            ' add items to listbox
            With lb
                .Location = New Point(10, 40)
                .Size = New Size(50, ClientSize.Height - 50)
                .Anchor = AnchorStyles.Left Or AnchorStyles.Bottom Or AnchorStyles.Top
                .DataSource = matrix
            End With
            Controls.Add(lb)
        End Sub
    End Class
    


    Regards Les, Livingston, Scotland

    Wednesday, May 1, 2019 7:13 PM
  • Hi

    test code:

    Module Module1
    
        Sub Main()
            Dim random As Random = New Random
            'm(1-9 random number) rows
            Dim m As Integer = random.Next(1, 10)
            'n(1-9 random number) columns
            Dim n As Integer = random.Next(1, 10)
            Dim arr As Integer() = New Integer(m * n - 1) {}
            For i = 0 To arr.Length - 1
                arr(i) = random.Next(0, 2)
            Next
            For j = 0 To arr.Length - 1
                If j <> 0 And (j + 1) Mod m = 0 And m<>1 Then
                    Console.WriteLine(arr(j))
                Else
                    Console.Write(arr(j))
                    Console.Write("  ")
                End If
    
            Next
            Console.ReadLine()
        End Sub
    
    End Module
    

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, May 2, 2019 2:48 AM
  • Thanks to all. It really solved my query. Regards.

    Tariq

    Thursday, May 2, 2019 11:03 AM
  • Hi,

    I am glad you have got your solution, we appreciated you shared us your solution and mark it as an answer.

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, May 3, 2019 1:01 AM