What am I doing wrong with random?

I have code that uses a random number to determine if an object is special or not. I use this code for each of the 4 objects that reset each so often.

Random rand = new Random();

int i = rand.Next(1, 25);

if (i == 1)
{
    thiss.typer = "boulder";
    thiss.texture = Content.Load<Texture2D>("rock");
}
else if (i == 2)
{
    thiss.typer = "ice";
    thiss.texture = Content.Load<Texture2D>("ice");
}
else if (i == 3)
{
    thiss.typer = "bomb";
    thiss.texture = Content.Load<Texture2D>("bomb");
}
else
    thiss.typer = "normal";

But every time I execute this code, when it creates 4 objects, when it makes one of them special, it makes them special for some reason. Is there something wrong with this code, or will I need to show more of my code to shed light on it?

+3
source share
1 answer

I can think of two reasons why this could happen.

Firstly, perhaps you accidentally exchange a link between your objects, so it thiss.typerrefers to the same object in all 4 cases.

-, , , , rand.Next . new Random() , . , .

Random, , , . , .

+14

All Articles