I have function which randomizes numbers:

public int losuj()
    Random losowanie = new Random();
    int los = losowanie.Next(0, 9);
    return los;

I want to fill array with numbers generated by losuj function, so I wrote:

for (int i = 0; i < 9; i++)
    for (int j = 0; j < 9; j++)
        planszaTab[i, j] = losuj();

Instead of array with random numbers all I get is array fill with the same number ex. 3. Each time I run program I get different number in which array is filled.

What I do wrong?

8 Years
Discussion Span
Last Post by ddanbe

Because its happening so fast the seed is the same for all.
move the

Random losowanie = new Random();

to outside the function so its not recreated each time, so its a private variable to your class. You should get different numbers then.


You want random numbers in the range from 0 to 9. Which is 10 possible outcomes.
The array in the for loops goes from 0 to 8, which is 9 places.
If that is what you want, OK with me. But I like to mention it anyway.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.