6 Replies - 356 Views - Last Post: 02 May 2019 - 07:00 AM Rate Topic: -----

#1 tariq75   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 01-May 19

Matrix in VB.net

Posted 01 May 2019 - 11:59 AM

I would like to generate random binary matrix (0 and 1) in vb.net for creating random population for genetic algorithm. 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. Any help will be highly appreciable.
Regards. Tariq
Is This A Good Question/Topic? 0
  • +

Replies To: Matrix in VB.net

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14994
  • View blog
  • Posts: 59,870
  • Joined: 12-June 08

Re: Matrix in VB.net

Posted 01 May 2019 - 12:00 PM

Remember this is a volunteer forum and it is frowned upon to ask people to do your work without showing any attempt on your own.

So.. what have you tried, thought about, or considered?
Was This Post Helpful? 0
  • +
  • -

#3 Sheepings   User is offline

  • Senior Programmer
  • member icon

Reputation: 223
  • View blog
  • Posts: 1,260
  • Joined: 05-December 13

Re: Matrix in VB.net

Posted 01 May 2019 - 01:47 PM

As you've been advised here on MSDN Forum. Use the random class and populate an array of numbers. (Find info on arrays in my signature.)

You would then infinite loop changing your UI by use of delegates. Why delegates? Because you will need to run your infinite loop in a new thread which takes the executed code and runs it external to your UI thread (so not to lock up your UI). And in order to access controls such as lables etc on your UI, you will need to use a delegate to send the values from your infinite loop back to your UI. I'd advise some light reading on thread-safe code and multi-threaded events. You can find a few tutorials on this in the tutorials section of this site.
Was This Post Helpful? 0
  • +
  • -

#4 tariq75   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 01-May 19

Re: Matrix in VB.net

Posted 02 May 2019 - 04:24 AM

View Postmodi123_1, on 01 May 2019 - 12:00 PM, said:

Remember this is a volunteer forum and it is frowned upon to ask people to do your work without showing any attempt on your own.

So.. what have you tried, thought about, or considered?


Thanks to all. I have 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

Was This Post Helpful? 0
  • +
  • -

#5 Sheepings   User is offline

  • Senior Programmer
  • member icon

Reputation: 223
  • View blog
  • Posts: 1,260
  • Joined: 05-December 13

Re: Matrix in VB.net

Posted 02 May 2019 - 06:01 AM

Glad you found what works for you. :) If you want it to run infinite, (for whatever reason), you can change the for loop to a do loop if needed. ;)
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14994
  • View blog
  • Posts: 59,870
  • Joined: 12-June 08

Re: Matrix in VB.net

Posted 02 May 2019 - 06:35 AM

@op - I would suggest, next time, doing the homework yourself and not having others write it for you. ;) :^:

http://www.vbforums....m-Binary-Matrix
Was This Post Helpful? 0
  • +
  • -

#7 Sheepings   User is offline

  • Senior Programmer
  • member icon

Reputation: 223
  • View blog
  • Posts: 1,260
  • Joined: 05-December 13

Re: Matrix in VB.net

Posted 02 May 2019 - 07:00 AM

To note, if you run the code you have acquired from VBForums in a console, it will stop after a while. See screenshot ::
Attached Image

But if you slightly alter that code; pseudo, it should run non stop without a glitch.

Imports System
Imports System.Threading

Module Program
    Sub Main(args As String())
        'BuildMatrix(100, 200)
        runMatrix(100, 200, True) ' The less the variable you pass to V2 (200) will decrease the width it uses in console. 
    End Sub
    Private spin As Integer
    Private mRand As New Random
    Private rndMatrix As Integer(,)
    Private Sub runMatrix(v1 As Integer, v2 As Integer, bool As Boolean)
        ReDim rndMatrix(v1, v2)
        Console.ForegroundColor = ConsoleColor.Green 'Change the console color if you desire
        While bool 'Runs once true, you can devise dome logic to stop it
            spin = +1
            Dim valu = mRand.Next(0, Integer.MaxValue)
            For y = 0 To v2
                rndMatrix(spin, y) = ((valu >> y) And &H1)
                Console.Write(String.Format("{0} ", rndMatrix(spin, y)))
            Next
            Console.Write(Environment.NewLine)
        End While
        Console.ForegroundColor = ConsoleColor.White 'Reset the color back to white while not looping
    End Sub
End Module

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1