0

I have to fill a matrix with randomic numbers from a set of (-1,0,1)... My code do this, however I would like that my matrix has more different elements inside of it. I try to explain myself better with an example.

One of the output of my code is:

-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
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0,0,0,0,1,1,1,-1,1,1,1,-1
0,-1,0,0,0,0,0,-1,0,0,0,1,-1,1,0,-1,0,0,0,0,0,0,0,-1,1,1,0,0,0,0,-1
0,-1,0,0,0,1,1,1,-1,0,0,1,1,1,0,-1,0,0,0,0,0,0,0,0,1,1,-1,0,0,0,-1
1,-1,1,0,1,0,1,1,0,0,0,1,1,0,-1,-1,0,0,1,0,0,0,0,1,1,1,0,1,-1,0,-1
0,-1,0,0,0,-1,-1,-1,1,1,1,0,-1,-1,-1,-1,1,1,0,1,0,1,0,-1,1,1,-1,0,-1,1,1
0,-1,0,0,0,-1,0,0,1,1,0,1,-1,-1,0,-1,1,1,0,1,0,1,0,1,1,-1,-1,0,0,1,1
0,-1,0,1,0,-1,0,-1,0,0,0,0,1,-1,1,-1,1,1,0,0,0,0,0,-1,0,1,-1,0,1,1,1
0,1,0,0,0,0,0,1,-1,1,0,0,1,0,-1,1,0,0,0,1,0,1,0,-1,0,1,0,0,0,0,-1
1,1,1,0,1,1,-1,1,-1,0,0,1,1,0,0,1,1,1,0,0,0,0,0,1,0,1,-1,0,1,1,-1
0,-1,0,0,0,-1,-1,-1,0,0,0,1,-1,0,-1,-1,0,0,0,0,0,0,0,-1,1,1,-1,0,0,0,1
0,-1,0,0,0,-1,-1,1,0,0,0,0,1,-1,-1,-1,0,0,0,0,0,0,0,-1,1,0,-1,0,0,0,1
0,-1,0,0,0,0,1,1,-1,0,0,0,1,0,1,-1,1,1,0,0,0,0,0,1,0,1,-1,1,-1,1,-1
0,-1,0,0,0,-1,1,1,0,0,0,1,1,0,-1,-1,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,1

So not a lot of the same rows like in my output.

But I don't have an idea how to do this... it is possible?

My code:

static void Main()
{
    int righe = 2432; //rows
    int colonne=31; //coloumns

    int[,] matrice = new int[righe, colonne]; 

    for(int i = 0; i<righe; i++) 
    {
        for (int y = 0; y < colonne; y++)
        {
            System.Random random = new System.Random();

            int number = random.Next(0, 2); 
            int[] choices = new int[3] { -1, 0, 1 }; 
            matrice[i, y] = choices[number]; //insert the random value
            Console.WriteLine(matrice[i, y]); 
        }
    } 
}
Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
JEricaM
  • 794
  • 2
  • 15
  • 37

2 Answers2

3

Move the System.Random random = new System.Random(); line outside the for loops. The random number generator is initialized (aka "seeded") with system time and if you call new Random() very often (like in your example), the time doesn't change enough. So you get the same random numbers.

Vilx-
  • 104,512
  • 87
  • 279
  • 422
1

Place System.Random random = new System.Random(); outside of the loops.

TobiasW
  • 378
  • 1
  • 5
  • 17
  • 1
    It's generally preferred to explain what's going on rather than just giving a recommendation, because we're trying to teach people things, not just fix an issue once and have it creep up again and again because they don't understand the underlying problem. See [the other answer](http://stackoverflow.com/a/31368307/1711796) for an example. – Bernhard Barker Jul 12 '15 at 13:37