Randomize the location of an LCD print

I have tried to find the answer to this question through the search function and I haven't found anything. I'm 100% certain I'm not asking it the right way, but maybe someone will read this and be able to help me out.

I'm new to programming, and I'm not great at it. However...I need to print a character to a random location on the lcd. That's easy enough.

lcd.setCursor(random(0,16), random(0,2));

Now...what I want to do is to create a variable for the row and column.

int randRow = random(0,16);
int randCol = random(0,2);

Then the command would be...

lcd.setCursor(randRow,randCol);

BUT, every time I do that, the lcd sets the cursor to (7,1) and it just stays there.

What the heck am I doing wrong?

Cannot see your complete sketch.

Try printing some variables to the serial monitor to see what’s happening.

randomSeed()

See:
https://www.arduino.cc/reference/en/language/functions/random-numbers/randomseed/

Sorry, I didn't include the whole sketch the first time around. Just to make sure i'm making sense, if I use (random(0,16), random(0,2)) for the set cursor command it works. It's this version here that does not.

#include <LiquidCrystal.h>
int randRow = (random(0,16));
int randCol = (random(0,2));
LiquidCrystal lcd(13, 12, 11, 10, 9, 8);

void setup()
{
lcd.begin(16, 2);
lcd.clear();
}

void loop()
{
lcd.setCursor(randRow,randCol);
lcd.print("h");
delay(200);
lcd.clear();
}


#include <LiquidCrystal.h>
byte randRow;
byte randCol;
LiquidCrystal lcd(13, 12, 11, 10, 9, 8);

void setup()
{
lcd.begin(16, 2);
lcd.clear();
randomSeed(analogRead(A0));    // leave A0 open circuited
randRow = (random(0,16));
randCol = (random(0,2));
}

void loop()
{
randomSeed(analogRead(A0));    // leave A0 open circuited
randRow = (random(0,16));
randCol = (random(0,2));
lcd.setCursor(randRow,randCol);
lcd.print(“h”);
delay(200);
lcd.clear();
}

Edit
Made a few changes above, to be tested.

no need to call
randRow = (random(0,16));
randCol = (random(0,2));
in your setup

Haha...so simple!

And what Kenny posted and then removed made sense as well. I wasn't thinking it all the way through. Basically, by setting the integers to a random number in the setup, it did the random selection once and then THAT number was what would be used down in the loop.

So...is the fact that the cursor would always place into the (7,1) spot a factor of the default pseudorandom code?

The OP had it there, I just left it.

Yes, when all is said and done the OP should always go through the sketch and remove extraneous code.

Thank you, to both of you. You got me through my block. I spent too much time trying to do the same thing over and over...couldn't see the forest for the trees.

That seems correct but we need to see the code that doesn't work. Seeing the code that works would be nice also.

Yeh I removed it because it was answered at the same time. The funny thing about random is that it is not really that random, if you start it at the same time in the same place you will find that the outcome is the same. So if you really want something that is absolutely random, that is not really the way. If you find it interesting, look up how random Math.random() is in C, the discussions and solutions are really nice to learn from.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.