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?

Recommended Answers

All 2 Replies

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.