Why dont my texture tiles draw as expected

Hello all, I have the following code bellow. Why dont each of my tiles get different colors?
When I start debugging, all my tiles gets the same colors…
I know it flashes since im drawing all the time, and each flash it changes color.
It seems to be getting one of the spriteBatch.Drawing() method, then draws all tiles, and when its done, it randomly get a new spriteBatch.Drawing() method and draw that untill the end… I want each tiles with random colors! :smiley:

I hope you guys understood me, if not ill try explain better. I know my code isent good, im just trying dfferent sorts of things :smiley:

protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.White);

            // TODO: Add your drawing code here

            for (int y = 0; y < screenHeight; y += 32)
            {
                
                for (int x = 0; x < screenWidth; x += 32)
                {
                    drawTexture(x, y);
                }
    
            }

            base.Draw(gameTime);
        }



        public void drawTexture(int x, int y)
        {
            spriteBatch.Begin();

            Random rand = new Random();

            int randomNum = rand.Next(0, 3);

            if (randomNum == 0)
            {
                spriteBatch.Draw(text, new Vector2(x, y), textSourceRectangle, Color.LightBlue, 0.0F, new Vector2(0, 0), 1.0F, SpriteEffects.None, 0.0F);
            }
            else if(randomNum == 1)
            {
                spriteBatch.Draw(text, new Vector2(x, y), textSourceRectangle, Color.LightGreen, 0.0F, new Vector2(0, 0), 1.0F, SpriteEffects.None, 0.0F);
            }
            else
            {
                spriteBatch.Draw(text, new Vector2(x, y), textSourceRectangle, Color.LightPink, 0.0F, new Vector2(0, 0), 1.0F, SpriteEffects.None, 0.0F);
            }

            spriteBatch.End();
        }

Its because rand.Next is based on your computer clock…

This means generating random numbers within the same millisecond or so, maybe within the same draw call, will be result in the same number again and again…

You should use the encryption based random rumber method… Its not very complicated, I think its maybe few lines.
I think if you search for “using encryption get random number” youll find something pretty fast…

This is only partially correct. rand.Next is not based on the computer clock, but the constructor rand = new Random() is.

That means, when you create a new Random instance in every drawTexture call you will most likely get the same random numbers.

You should call rand = Random() once when you initialize the game and then use the same instance in every drawTexture call.

In other words:
The following code produces the same random number in most cases.

var rand = new Random();
int randomNum = rand.Next(0, 3);

rand = new Random();
randomNum = rand.Next(0, 3);

rand = new Random();
randomNum = rand.Next(0, 3);

The following code produces different random numbers.

var rand = new Random();
int randomNum = rand.Next(0, 3);
randomNum = rand.Next(0, 3);
randomNum = rand.Next(0, 3);

Nice!

I get it… I was about to post several follow up questions, but it seems as I type them, they answer themselves…

Thanks alot guys!
That solved my problem :smiley: