Hi Ardupeople, Has anyone seen an example of 1d arrays of random integers? Any advice is appreciated.
Thank you.
KB
Hi Ardupeople, Has anyone seen an example of 1d arrays of random integers? Any advice is appreciated.
Thank you.
KB
You need to use Arduboy2::initRandomSeed()
in the setup()
loop to seed the random number generator.
After that you can use the random() function:
https://www.arduino.cc/reference/en/language/functions/random-numbers/random/
Otherwise you might find that your ārandomā bunch of numbers is always the same random bunch.
You should be able to use direct initialization uniform initialization calling random()
for each int in the list. Or you could declare the array and iterate through it assigning the result of random.
It would be interesting to know what you need them for, this could be an X-Y problem.
(I.e. asking how to implement a chosen solution rather than asking what the best solution is to the actual problem.)
What @Freezingsnail says is right, you just need to arduboy.initRandomSeed()
(preferably after a titlescreen so thereās a random delay before itās called), and then fill an array with the result of random
.
(I think rand
would work too.)
E.g.
constexpr size_t arraySize = 16;
uint8_t array[arraySize];
void randomiseArray()
{
for(uint8_t index = 0; index < arraySize; ++index)
{
array[index] = random();
}
}
Are you using ādirect initialisationā in its technical sense?
If so Iām not sure what you mean by this.
I mean like int randnums[2] {rand(), rand()};
That might not work though Iāve never tried it.
Actually I think I mixed the names up of what type of initialization that is.
Yea I meant uniform, my mistake.
Technically speaking thatās aggregate initialisation, which itself is a form of list initialisation.
But the technical jargon isnāt really important here.
(Frankly I can never remember the difference between initialisation types, I always look it up.)
The important thing is that while that would work if the array was local, if the array were statically allocated (i.e. global) then rand
would be being called before main
even begins,
so youād probably get the same sequence every time.
Besides which, if you had a 20 element array, calling rand
20 times would be a bit tedious to type and would probably be compiled into either a loop or (worse in this case) an unrolled loop anyway, so writing a loop is a better option.
Thank you for the replies. As to whether it is an X-Y problem, perhaps. Hereās some more detail: Back in the 80ās in BASIC I wrote a simple graphics routine where I created a 1D array of random integers, then used the graphics commands āplotā and ādrawā to use those random integers as X and Y Cartesian coordinates.The result was I was drawing a riverbank, sort of like river raid. So I am looking to re-create that effect.
Ahā¦
In that case itās sort of half an X-Y problem.
You can use a 1D array of random numbers like you did back then so asking about that makes sense,
but you still need an equivalent of āplotā and ādrawā.
I canāt really suggest an equivalent without knowing what those commands do/did.
The ZX81 book I just happen to conviniently have lying around suggests that:
PLOT m, n Blacks in the pixel (|m|, |n); moves the PRINT position to just after that pixel.
But it makes no mention of a DRAW
command.
QB64 has a DRAW
statement, which seems to be a complex precursor to SVG paths.
Iām guessing on the Arduboy you could use arduboy.drawPixel
and arduboy.drawLine
to simulate what these would have done, but I canāt be certain.
Also, if the points need to be within a particular range then the random
call will need extra arguments.
E.g. to generate points that only appear on screen:
struct Point2
{
uint8_t x;
uint8_t y;
};
constexpr size_t pointCount = 16;
Point2 points[pointCount];
void randomisePoints()
{
for(uint8_t index = 0; index < pointCount; ++index)
{
points[index].x = random(0, WIDTH);
points[index].y = random(0, HEIGHT);
}
}
Nice. yes, the Arduboy.drawLine is indeed what I intended to use. And you are also correct that I will need to range the values. That said, is my emulating an old technique well, sub-optimal?
I guess I am prototyping in my mind in BASIC, because thatās what I am most familiar with:
store an array of 100 random integers in memory using a for-next loop then use another for-next loop to plot a few x,y points and draw lines connecting them.
Not necessarily.
Drawing shapes can be quite processor intensive,
but at the moment Iām assuming thereās no other way to do it.
I donāt know what the final thing is supposed to look like.
Iām presuming thatās all there is to it - generate a series of random points and then draw lines between them.
In C++ itās just a for
loop.
C++ uses braces to indicate scope rather than an end keyword.
On the Arduboy you wonāt need to plot the points, you can just draw the lines using the data.
Iām assuming this is the sort of thing youāre trying to do:
#include <Arduboy2.h>
Arduboy2 arduboy;
struct Point2
{
uint8_t x;
uint8_t y;
};
constexpr size_t pointCount = 16;
Point2 points[pointCount];
void randomisePoints()
{
for(uint8_t index = 0; index < pointCount; ++index)
{
points[index].x = random(0, WIDTH);
points[index].y = random(0, HEIGHT);
}
}
void drawPoints()
{
Point2 previousPoint = points[0];
for(uint8_t index = 1; index < pointCount; ++index)
{
Point2 currentPoint = points[index];
arduboy.drawLine(previousPoint.x, previousPoint.y, currentPoint.x, currentPoint.y);
previousPoint = currentPoint;
}
}
void setup()
{
arduboy.begin();
}
void loop()
{
if(!arduboy.nextFrame())
return;
arduboy.pollButtons();
if(arduboy.pressed(A_BUTTON))
{
arduboy.initRandomSeed();
randomisePoints();
}
arduboy.clear();
drawPoints();
arduboy.display();
}
But Iām guessing you want the points sorted by x so they draw a line across the screen instead of jumping aboutā¦
In which case, something more like this:
#include <Arduboy2.h>
Arduboy2 arduboy;
struct Point2
{
uint8_t x;
uint8_t y;
};
constexpr size_t pointCount = 16;
Point2 points[pointCount];
void randomisePoints()
{
constexpr uint8_t xStep = (WIDTH / pointCount);
int16_t x = 0;
for(uint8_t index = 0; index < pointCount; ++index)
{
points[index].x = (x + random(-4, 4));
points[index].y = random(0, HEIGHT);
x += xStep;
}
points[0].x = 0;
points[pointCount - 1].x = (WIDTH - 1);
}
void drawPoints()
{
Point2 previousPoint = points[0];
for(uint8_t index = 1; index < pointCount; ++index)
{
Point2 currentPoint = points[index];
arduboy.drawLine(previousPoint.x, previousPoint.y, currentPoint.x, currentPoint.y);
previousPoint = currentPoint;
}
}
void setup()
{
arduboy.begin();
}
void loop()
{
if(!arduboy.nextFrame())
return;
arduboy.pollButtons();
if(arduboy.pressed(A_BUTTON))
{
arduboy.initRandomSeed();
randomisePoints();
}
arduboy.clear();
drawPoints();
arduboy.display();
}
Wow, that is excellent. Thank you. Let me poke it, prod it and mess with it in order to understand it fully.