I have tried long and hard at this, but just can’t seem to get anywhere.
I’m trying to make my 2d top-down background tilemap randomized either 1 - on. or 0 - off. 16 times across and 8 times down in a grid. Each tile based on its x and y position + the camera offset is input into the randomSeed() function.
However no matter how I set it up i can never get random values. Its either all repeating patterns or not random at all.
You should only be setting randomSeed once in the program or when you need to explicitly reset the random sequence to a known pattern. Each subsequent call to random() will give a new random value that should be poorly correlated with the previous one.
It would be helpful if you told us more about the actual reasoning behind why you want to do this/what effect you’re trying to achieve to avoid the XY problem.
It sounds like what you really want is to have some kind of procedurally generated map or pattern, but we don’t really know enough to be able to suggest something that’ll actually be useful for what you want.
You’re asking us about rendering a map, but I suspect what you actually want is to be able to generate the map, not merely to render it.
If, however, you’re only generating a background pattern and it’s only decoration, then you might find something like this useful:
Right. I suppose I’m more asking about generating a map. But because it’s infinite I can’t generate the whole thing.
So because I render everything I have generated I guess it’s combined. Altho maybe there is a better way of doing that instead.
I could generate chunks but I’d still need to generate them the same way.
As of right now it’s purely decoration, but the plan was to use the same method for generating trees and stuff.
If you’re expecting the player to be able to modify the map you’re going to struggle because the Arduboy has a very small amount of RAM (over 1,048,576 times less than your average desktop/laptop/smartphone) so there’s a limit to the number of modifications you can track. You’ll likely only be able to save a very small number of modifications, so the map will be constantly resetting itself to its original state.
Theoretically you could save off-screen chunks onto the FX chip if you’re writing this for the Arduboy FX, but that’s easier said than done.
If you don’t need the player to modify the map then life gets a heck of a lot easier, depending on what your other limitations are (e.g. whether the player should be able to interact with the map, e.g. colliding with trees and the like).
Infinite without repetition is going to be effectively impossible on the Arduboy because of its constraints.
Depending on your other constraints you can likely manage ‘large enough that nobody will notice that the map repeats’, but not ‘truly infinite without repetition’.
Depending on what kind of pattern you’re expecting/hoping for that’s also easier said than done.
One thing to note here is that you likely don’t want true/pure randomness. If you’re attempting to create a world then you probably want some kind of pattern because natural terrain does form patterns (e.g. rivers, height levels, trees growing in clusters).
With some good use of algorithms you can likely take a hash function as input and produce some convincing infinite terrain without too much CPU strain or memory usage, which would be fine if you’re happy for the terrain to be read-only, but needing to modify it would be an issue.
Some other code examples that may be of interest…
Generation of terrain using ‘noise’ and turbulance:
(A result of this old thread, particularly the culmination of the work here. You may want to skim that, particularly the parts about hash functions.)