Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
iam write this code for Matrix 1024*10
i want fill matrix with 0 and 1 with Random
With the Condition that there is no repeat(duplicate) row
This example is incorrect
1
HTML
0 1 0 1  1 0 1 0 0 1 
0 1 1 0  1 0 0 0 0 0
0 1 0 1  1 0 1 0 0 1 
.
.
.


C#
int d = 0 ,g=0;
           string s = "";
           string[] arrayList1=new string[1024] ;
           int n = 1024, m = 10;
           System.Random random = new System.Random();
           double[,] array = new double[n, m];
           for (int i = 0; i < 1024; ++i)
           {
               for (int j = 0; j < 10; ++j)
               {
                  array[i, j] = random.Next(0, 2) ;
                  s =s+  array[i, j].ToString();
                  Console.Write("  " + array[i, j].ToString() );
                 }



           }
           Console.WriteLine(g);
           Console.ReadLine();

           Console.ReadKey();

       }
Posted

A pragmatic Approach :)

If I really understand your question in the right way you like to make a Matrix with 1024 rows filled with lines having 10 columns which should contain only '1' or '0'.

That means every pattern for binary representation will be there in your 1024 rows, because 2^10 = 1024.

If the above is right, simply create a Matrix with binary represantation of 0...1023 as columns and ramdomly exchange rows about 1024 times.

N.B: It is more MxN Matrix then a NxN.
 
Share this answer
 
v5
Comments
saeed1364 30-Oct-14 15:05pm    
2^3=8
0 0 0
1 1 1
1 0 0
0 0 1
1 0 1
1 1 0
0 1 0
0 1 1
[no name] 30-Oct-14 15:08pm    
I don't get the Point, sorry. I see a 8x3 Matrix with no "duplicate rows" and every pattern is there. From my Point of view exactly the same you asked for your 1024x10 Matrix...where I'm wrong?
PIEBALDconsult 30-Oct-14 17:26pm    
I think that's the point. You don't need to generate random numbers, you only need to randomize the order of the numbers.
[no name] 30-Oct-14 17:30pm    
Thank you for confirmation to a noob's answer ;) (or maybe I missunderstood). My Point is also Shuffle the rows is the solution.

Still I don't understand what OP "Claims" with his/hers comment. Do you have an idea?
Regards, Bruno

To avoid any misinterpretation; I extended/precised my original comment after the comment of http://www.codeproject.com/script/Membership/View.aspx?mid=2587207[^]
Maciej Los 31-Oct-14 17:04pm    
Wow!
5ed!
I wouldn't do it bit-by-bit; I'd generate 32 bits at a time. You'll need to generate 10x32 integers (1024=32x32), then just plug them into the array.

Algorithm:
0) Instantiate a HashSet<int> and a Queue<int>
1) Get Random.Next()
2) If the new int is not in the Hashset, then add it to the Hashset and Queue
3) If fewer than 320 ints in the Queue, goto 1
4) Iterate 10 rows by 32 ints per row
5) Dequeue the next int
6) Get the base-2 representation of the value System.Convert.ToString ( intval , 2 ).PadLeft ( 32 , '0' )
7) Append (see StringBuilder) the string to the current row


A limitation of this technique is that the most-significant bit of each int will always be zero. If that is a concern, you could do sixteen bits at a time instead. Or try NextBytes:
http://msdn.microsoft.com/en-us/library/vstudio/system.random.nextbytes(v=vs.100).aspx[^]


I would also like to point out that the larger the things compared, the smaller the difference is required. So if you produce 10 full 1024-bit values, then any two values may differ by only one bit (although that has a small probability).
By generating 320 32-bit values I believe the resultant 1024-bit values are guaranteed to differ by at least 32 bits. And if you use 64 16-bit values, then you can expect at least 64 differences.
If the number of differences matters, then you may need to calculate the Levenshtein Distance and compare to some threshold rather than a simple equal/not-equal check.

P.S. I see that I got the shape of the array reversed, no big deal. Generate all the values from 0 to 1023 and then put them into the array in random order:

C#
int[] a = new int [ 1024 ] ;
for ( int i = 0 ; i < a.Length ; i++ ) a [ i ] = i ;
System.Collections.Generic.List<string> s =
  new System.Collections.Generic.List<string> ( a.Length ) ;
foreach ( int i in a.OrderBy ( x => System.Guid.NewGuid() ) )
  s.Add ( System.Convert.ToString ( i , 2 ).PadLeft ( 10 , '0' ) ) ;
 
Share this answer
 
v6
Comments
saeed1364 30-Oct-14 16:19pm    
please write code
PIEBALDconsult 30-Oct-14 16:22pm    
Heck no.
Maciej Los 31-Oct-14 17:03pm    
Goooaaaalllll!
+5!
PIEBALDconsult 31-Oct-14 17:08pm    
:D
The idea is: create a raw randomly and then check up if this row is already found in the matrix. If it is already found, try with random generation again, repeat until you get unique raw.

But how to use that better. First, define class representing the raw (a 1-dimensional array could be one of its members) and define the comparison methods for it. It will require to override the methods Equals, which will also requires overriding GetHashCode. Also, it might be useful to define custom operators '==' and '!=' (it requires to define both).
Please see:
http://msdn.microsoft.com/en-us/library/bsc2ak47(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/system.object.gethashcode(v=vs.110).aspx[^].

Why? It will allow you to store instances of this class as the keys of System.Collections.Generic.Dictionary or :
http://msdn.microsoft.com/en-us/library/xfhwa508%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/bb359438%28v=vs.110%29.aspx[^].

Why? These classes force uniqueness of the key and provide O(1) time complexity for the check of uniqueness. You add an element of your key class with the row, along with adding the row to a matrix. This redundant data will help you to check up uniqueness of the next row.

See also:
http://en.wikipedia.org/wiki/Big_O_notation[^],
http://en.wikipedia.org/wiki/Time_complexity[^].

—SA
 
Share this answer
 
Comments
Maciej Los 31-Oct-14 17:08pm    
Your answer, Sergey, convinces me ;)
+5!
Sergey Alexandrovich Kryukov 31-Oct-14 18:55pm    
Thank you, Maciej.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900