So I decided to go back to basics and do some small code challenges/projects to find out where my knowledge gaps are. I started with Game of Life, but there’s something off about my code and I’m not sure what it is. The seed looks exactly the same every time I run the program, despite me using rng.Next
(0,2) > 0
to generate a random status for each cell in the beginning.
I’ve chucked my code into Pastebin here – anyone mind giving me a code review?
https://pastebin.com/5Ltk2RA8
i believe you have to decide the fate of each cell at the same time and not one after the other. the way you coded it, the cells may check against some cells that are already updated to the next generation
Ah, that makes sense. Thank you for the help!
I would suggest you 3 things:
Make the Random class instance persistent across the entire simulation, instead of recreating it on each step. You could create the instance on the main method for example, and use it to constantly seed values to the simulation. This way you will also make the entire simulation deterministic and dependant on a single seed value specified at the very beginning.
Use the current system Ticks count as a seed value when instantiating the Random class (assuming you won’t supply your own). This will give you better Random results, since .NET tend to use the less precise system timer by default.
Give the Random class more margin for generating results when deciding if a cell lives or dies, instead of generating only ones and zeroes, you could generate a float number between 0 and 1 and check if its greater than 0.5f. Sounds silly but in my experience this way the results are better distributed. If you pick a very narrow range for the Random class it will deviate to either side. Don’t know if this still holds true with today’s. net versions but won’t hurt neither.
Hope that helps
Interesting, I didn’t think about it that way. But yes, your third point makes sense because the higher the range the more random it’s going to be. Thank you so much!
(2) is irrelevant if you do (1). The RNG is a giant lookup table, and the seed determines where you start in the sequence. Using a different seed only affects where you start, it doesn’t make your results “better”.
Besides, the default uses the tick count anyway.
(3) is probably not completely true, but is harmless. In general I prefer this approach anyway.
The Random is working fine. What appears to be happening is the rows are being duplicated on each row. This is because each row is the same instance, since you only create one and assign it to each slot in the grid.
So what happens when you Seed “each” row, you are really updating the one instance that is copied across all grid slots, so the “last” seeded row overwrites it.
What you need to do is create a new row for each grid slot. Move the initialization of the row into the grid loop.
Also, add
Or something.
A console window is less than 50 characters high by default, so it scrolls up because it doesn’t fit.
Console characters are 8 x 16 (?) at any rate they’re not square, so making a console grid square in dimensions makes in rectangular visibly.
This part of RunSimulation causes the simulation to only run on the lower-left half diagonal of the grid.
j should iterate over the entire row, right?
Isn’t rng.next(0,2)>0 always true? Don’t you mean >1 ?