Generate random number doesn't work?

I want to generate a random number between 1 and 2. For that I use:
srand(time(0));
randomnumber = rand() % 2 + 1
But it always returns me the number 2. What is the best way to generate random numbers?

1 Like

I modified the hello world example to get this:

#include "Pokitto.h"

Pokitto::Core mygame;

int main ()
{
	mygame.begin();
	srand(time(0));
	while (mygame.isRunning())
	{
		if (mygame.update())
		{
			auto r = rand() % 2 + 1;
			mygame.display.print(r);
		}
	}
}

And it worked.

So I’d inclined to believe that either you’re always calling srand just before you call rand (you’re only supposed to call srand once because it resets the sequence), or for some reason something else is affecting your results, or perhaps time isn’t working as it should on whatever you’re doing.

If you showed the rest of your code it might become clearer what the problem is.

2 Likes

Yeah I noticed that its a different problem. I make a game where you breed an egg at the start and after it should be random if you get a male or female from the egg. Thats why I need a random number. First I thought that random number doesn’t work but after I set random number to a fix value it still didn’t work. So here is my code, I hope I made a simple mistake (Im new to C++):

Graphic.h:

#include <stdint.h>

const uint16_t graphic_palette[] = {61450,65535,0,65351,64164,53605,62226,43575,52967,36361,6884,1855,951,31401,40179,16904};

const uint8_t background[] =
{
110,88,
204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,
204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,
204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,
204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,
204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,
204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,
204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,
204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,
204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,
204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,
204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,
204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,
204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,
204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,204,
34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,
153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,
153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,
153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,
153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,
153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,
153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,
153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,
153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,
153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,
136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,
136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,
136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,
136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,
136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,
136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,
136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,
136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,
136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,
153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,
153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,
153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,
153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,
153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,
153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,
153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,
153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,
153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,
136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,
136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,
136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,
136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,
136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,
136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,
136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,
136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,
136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,
153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,
153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,
153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,
153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,
153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,
153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,
153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,
153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,
153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,
136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,
136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,
136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,
136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,
136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,
136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,
136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,
136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,
136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,
153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,
153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,
153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,
153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,
153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,
153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,
153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,
153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,
153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,
136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,
136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,
136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,
136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,
136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,
136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,
136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,
136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,
136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,153,153,153,153,153,136,136,136,136,136,
34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34,34
};

const uint8_t pokochi_male_right[] =
{
22,18,
0,0,0,0,0,0,0,0,0,0,0,
0,0,2,34,34,34,34,34,0,0,0,
0,0,34,34,34,34,34,34,32,0,0,
0,2,34,34,34,34,34,34,34,0,0,
0,34,34,34,34,34,34,34,34,32,0,
2,34,34,34,34,34,34,17,34,34,0,
34,34,33,18,34,34,34,17,34,34,32,
34,34,33,18,34,34,34,34,34,34,32,
34,34,34,34,34,34,34,34,34,34,32,
34,34,34,34,33,17,18,34,34,34,32,
34,34,34,34,34,34,34,34,34,34,32,
34,34,34,34,34,34,34,34,34,34,32,
34,34,34,34,34,34,34,34,34,34,32,
34,34,34,34,34,34,34,34,34,34,32,
34,34,34,34,34,34,34,34,34,34,32,
34,34,34,34,34,34,34,34,34,34,32,
34,34,34,34,34,34,34,34,34,34,32,
2,34,34,34,34,34,34,34,34,34,0
};

const uint8_t pokochi_female_right[] =
{
22,18,
0,0,0,0,0,0,0,0,0,0,0,
0,0,1,17,17,17,17,17,0,0,0,
0,0,17,17,17,17,17,17,16,0,0,
0,1,17,17,17,17,17,17,17,0,0,
0,17,17,17,17,17,17,17,17,16,0,
1,17,17,17,17,17,17,34,17,17,0,
17,17,18,33,17,17,17,34,17,17,16,
17,17,18,33,17,17,17,17,17,17,16,
17,17,17,17,17,17,17,17,17,17,16,
17,17,17,17,18,34,33,17,17,17,16,
17,17,17,17,17,17,17,17,17,17,16,
17,17,17,17,17,17,17,17,17,17,16,
17,17,17,17,17,17,17,17,17,17,16,
17,17,17,17,17,17,17,17,17,17,16,
17,17,17,17,17,17,17,17,17,17,16,
17,17,17,17,17,17,17,17,17,17,16,
17,17,17,17,17,17,17,17,17,17,16,
1,17,17,17,17,17,17,17,17,17,0
};

main.cpp:

#include "Graphic.h"

enum Sex {male, female};
Sex sex;
uint8_t positionX;
uint8_t positionY;

void handlePokochi()
{
    uint8_t maxFrameCount = 10;
    uint8_t frameCount = maxFrameCount;
    positionX = 30;
    positionY = 30;
    
    // For test purpose I set the random number to 1
    uint8_t randomNumber = 1;

    switch (randomNumber)
    {
    case 1:
        sex = male;
        break;
    case 2:
        sex = female;
        break;
    }

    while (true)
    {
        if (game.update())
        {
            if (frameCount == maxFrameCount)
            {
                game.display.drawBitmap(0, 0, background);

                switch(sex)
                {
                case male:
                    // Draw male
                    game.display.drawBitmap(positionX, positionY, pokochi_male_right);
                case female:
                    // Draw female
                    game.display.drawBitmap(positionX, positionY, pokochi_female_right);
                }

                frameCount = 0;
            }

            frameCount++;
        }
    }
}

But the funny thing is that it always draws the female, so although the number is 1, it still thinks the sex is female… The code isn’t complete so it won’t work. There must be something wrong with the sex part.

2 Likes

You’re missing a break; before case female:!
Otherwise the execution will execute the female case as well (which is brought up by my compiler as a warning usually by the way).

By the way, that might be the intended way (for example, when you need the same behavior for the same cases, and when defaulting to a known case too).

4 Likes

Thank you so much!!!:relaxed:

2 Likes

@carbonacat beat me to it because I was busy today.

@zer0 I’ll give you a little tip though:

You would be better off using 0 and 1 instead of 1 or 2 for your cases.

Offsetting it by 1 might be humanly easier to read if you aren’t used to zero-based indexing yet, but keeping it as 0 and 1 will be easier for the computer (the Pokitto) to work with.

In fact you could do something like this:

enum Sex
{
  Male = 0,
  Female = 1,
};

Sex getRandomSex(void)
{
  return static_cast<Sex>(rand() % 2);
}

Then instead of that first large switch statement you can do:

sex = getRandomSex();

(I’d also like to suggest using an enum class instead of a plain enum, but that would take some explaining.)


Yes! Warnings are your friend, eliminate them whenever you see them.

I try to make sure my code doesn’t produce any warnings.
(Unfortunately there’s a lot of warning noise from the library at the moment, so it can be hard to spot.)

Case fallthrough is generally considered bad in almost any case.
It makes the code harder to read and can sometimes hurt performance.
Some modern languages forbid it entirely or only permit it by using explicit goto statements.

If you’re trying to use case fallthrough, you should really stop and think “is there a better way I could do this?”.
(You don’t want to end up writing a version of Duff’s Device.)

2 Likes

I agree! I rarely use it myself, or never at all. And that’s a pain to maintain anyway haha

Goto gives me shivers too

1 Like

Whats is the difference if I say for example Sex(0) and cast? Why should I use enum class instead of a normal enum?

Sex(0) is equivalent to (Sex)0, which is a C style cast.
C style casts are different from static_cast.
There’s a nice comprehensive explanation of all the different cast types here.

(Sex(0) is only a C-style cast in this case because it’s an enumeration type. If it was a class or a struct, that would call its constructor.)

Firstly because unscoped enumerations (plain enum) can result in name clashes.
Slightly modifying an example taken from here:

// this code won't compile!
enum Colour { Red, Green, Blue }; 
enum Feeling { Excited, Moddy, Blue };

The two cases of Blue clash with each other because plain enumerations aren’t scoped.

If you change it to a scoped enumeration:

enum class Colour { Red, Green, Blue }; 
enum class Feeling { Excited, Moddy, Blue };

Then it works properly because the different enum values have to be accessed through the name of the enum type (i.e. they are scoped).
So Colour::Blue doesn’t clash with Feeling::Blue.

Secondly, type safety.
Unscoped enumeration values are implicitly convertible to and from integer types, so this can happen:

// Not a valid value but it compiles anyway
Colour colour = 200;

A scoped enumeration is strongly typed, so you’d get a compiler error instead

// Error, integer not implicitly convertible to Colour
Colour colour = 200;

Although you can still override that if you genuinely mean it.

// You'd have to actually be trying to get this wrong on purpose
Colour colour = static_cast<Colour>(200);

I’m currently gathering interesting things like these into my resource collection.

2 Likes

This is one of the best explanations i have read for the need in “::”, nice post Pharap.

So many examples use abstract code to explain the concept but as you follow this whole topic this makes a lot of sense, using the colour and an emotion as comparrison makes it much clearer as to why the difference is important; that part is usually confused when an author uses stuff like This::That, Fuu::Bar or A::B - they always looked strange and original programs had no real purpose or goal.

1 Like

Thanks.

Technically scoped enumerations are just one case of scoping, but yeah, the same principle applies: avoiding name clashes.

To give a Pokitto-specific example, Pokitto::Buttons, Pokitto::Display and many other Pokitto classes have a begin function (e.g. Pokitto::Buttons::begin() and Pokitto::Display::begin()).
If it weren’t for scoping, the names would clash and it would be impossible to tell which begin you were trying to use.

By the way, the technical name for :: is the “scope resolution operator” (an informally it’s sometimes called the “double colon operator”).

I know what you mean.
I think that’s because experienced programmers tend to think more abstractly, but they often forget that less experienced programmers haven’t gotten to that point yet, hence they end up with ‘foo’ and ‘bar’ everywhere instead of coming up with a tangible example.

Even a slightly convoluted example can work (like the case here - a Feeling enumeration isn’t something you’d come across every day, let alone needing a Colour enumeration at the same time).