[SOLVED] Random not working properly?

I think this is more of a C# question than a monogame question

I have a class called Tile

I have a list of those called Tiles

in each Tile’s constructor, there’s a random generator to make each tile a different frame.

However, it doesn’t seem to be working properly. It only makes it one frame, it doesn’t pick a new random frame for each Tile, it just picks one and does that for each new Tile. what’s wrong here?

If Index == 0 perhaps? you are only checking for 1 input…

oh, no. index always is 0 for each tile that im inputting. it’s supposed to be like that. that has nothing to do with it.

Ah sorry, just woke up and thought you were changing the value of index, just noticed currentFrame there…

Might help if you posted the code as text though so someone can quickly test the code for you…

Where is currentFrame defined?

It’s initially 0. It’s defined here at first

Then I define it in the constructor randomly, which works, but it only picks ONE random int for every single Tile in the Tiles list. I want each individual tile to be their own random currentFrame instead of the first one it picks

1 Like

Looks like you are initialising Random each frame. Remember the Random class is not a true random, for the same time it will always generate the same range.

Move the :

Random rand = New Random();

To the top of the class and that should help. However to make random a bit more random, use a modulus as well, something like (excuse coding from memory)

int r = random.Next(32235) % 4;

Hope that helps.

1 Like

Seeding, was going to mention that :slight_smile:

You are making a new Random class each time and are most likely using the same seed. Explained a bit more here.

Basically you are just using the same initial random number due to the seed being created at the same time.

I am not 100% sure but I feel this is most likely the case but it depends on your implementation. I am assuming you have a loop that calls all those Tile constructors really quickly, so they are all using the same time based seed.

Solution: I suggest starting the Random class just outside the initial loop so it is only initialized once.

It worked, thank you!

@SimonDarksideJ Beat me by 4 mins :stuck_out_tongue:

@James_Maslaki A RNG is sometimes an excusable case for using a global variable.