Hello,
I am trying to understand arrays. I'm trying to randomly place and store plotted points on a 24x16 matrix. I need to go back and read which lights are lit and which are not from the array. Will this work to randomly place the plotted points?
Does this look right?
void populateMatrix()
{
for (byte x = 0; x < 24; x++)
{
for (byte y = 0; y < 16; y++)
{
conwayMatrix[x][y] = (byte)random(2);
}
if (arrayMatrix[x][y] == 1)
{
matrix.setPixel(x,y);
}
}
}
Apart from the use of variable y outside of its scope?
Well, there is that. But, that has nothing to do with the question. The array WILL be populated correctly. The correspond set of pixels that are turned on won't match the array contents, but that's a different issue.
void populateMatrix()
{
for (byte x = 0; x < 24; x++)
{
for (byte y = 0; y < 16; y++)
{
int b = (byte)random(2);
conwayMatrix[x][y] = b;
if (b) matrix.setPixel(x,y);
}
}
}
Game of life ?
The random patterns become more interesting if the generator looks at neighbour pixels to affect the chance.
Thanks for all your help so far.
Yes it is the Game of Life. I was trying to understand how it all worked and not just post the sketch and ask someone to fix it.
With the new Random patters it works well now but when I turn it off and back on it generates the same pattern.
Not sure why. I wish I could debug better not my strong suit. I would like to see what the checking neighbors loops are doing.
I think the loop is checking each pixel to see if there is a neighbor.
So now does the pixels (lit/not lit) correspond to the array contents with the new random?
That would help checking neighbors for sure.
The sketch is quite small 7890 bytes I get the felling the array is not filling up. I will study it agin.
Using a few C tricks that can be made more succinct
although whether that's good style is definitely open to question
void populateMatrix()
{
for (byte x = 0; x < 24; x++)
for (byte y = 0; y < 16; y++)
if (conwayMatrix[x][y] = random(2))
matrix.setPixel(x,y);
}
Note that usually I only omit braces for if/for/while if the body is a single statement
on a single line - I certainly wouldn't do it for nested if's, but I like brevity.
Side-effects in a condition like this are a trap for the unwary of course - never confuse
= and ==
With the new Random patters it works well now but when I turn it off and back on it generates the same pattern.
The random generator is deterministic software, it generates the same sequence every time.
You can let it start at a different point in the sequence by setting the randomSeed.
by initializing this by a floating analog input you get a bit more randomized version.
a better initialization can be done by letting the user press a button twice
and use the time in micros in between to seed the random generator
With the new Random patters it works well now but when I turn it off and back on it generates the same pattern.
The random generator is deterministic software, it generates the same sequence every time.
You can let it start at a different point in the sequence by setting the randomSeed.
by initializing this by a floating analog input you get a bit more randomized version.
a better initialization can be done by letting the user press a button twice
and use the time in micros in between to seed the random generator
source from - http://arduino.cc/en/Reference/randomSeed -
Thanks Rob that helps me understand that better. I thought it did give me a different random pattern every time. Now to get the array to match the random plots being generated and I am almost there.
but as analogRead has only 1024 distinct values you have at most 1024 starting points (in practice maybe 30).
That is why you should "harvest randomeness" aka entropy from user interaction.