By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,474 Members | 939 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,474 IT Pros & Developers. It's quick & easy.

i want to generate a matrix of 0's and 1's which is random every time plz hlp...!!

P: 5
i want to generate a series of random numbers like


it is a 5*5 matrix and is random every time upon execution
my code is:-

Private Sub Command1_Click()

Call Randomize
Dim x As Integer, y As Integer, z As Integer, t As Integer, s As Integer
Dim i As Integer, k As Integer
x = 0 + Int(2 * Rnd())

y = 0 + Int(2 * Rnd())

z = 0 + Int(2 * Rnd())
t = 0 + Int(2 * Rnd())

s = 0 + Int(2 * Rnd())
Print x, y, z, t, s

End Sub

but i have to click on command button each time to generate a random 0's and 1's and i want to print random 5*5 matrix in a single click as said above but in this code when i m clicking the command button i m getting a 1*5 random matrix and i have to click again and again.....can i use goto control and send the cursor to initialize again and again and print till i get a 5*5 matrix or use for loop.. but i have tried everything and it isn't working....please help me get 5*5 random matrix of 0's and 1's.......
Nov 7 '09 #1
Share this Question
Share on Google+
1 Reply

Expert 100+
P: 305
Use a nested for loop structure...
Expand|Select|Wrap|Line Numbers
  1. Dim OuterForLoopCounter As Integer, InnerForLoopCounter As Integer
  2. Dim Result As Integer, MyMatrix(1 To 5, 1 To 5) As Integer
  3. For OuterForLoopCounter = 1 To 5
  4.   For InnerForLoopCounter = 1 To 5
  5.     Result = Int(2 * Rnd)
  6.     MyMatrix(OuterForLoopCounter, InnerForLoopCounter) = Result
  7.   Next InnerForLoopCounter
  8. Next OuterForLoopCounter

Good Luck
Nov 8 '09 #2

Post your reply

Sign in to post your reply or Sign up for a free account.