declare between two values

Hey! Did not find any topic about this more than using if statment.
I want to declare a middle position and that would be between 0 and 3000. How do I write this? Should I use a variable insted?

int top_position = 0;
int bottom_position = 3000;
int middle_position = ??

Could you clarify what is a middle position and how you are gong to use it?

alesam:
Could you clarify what is a middle position and how you are gong to use it?

Between the positions 0 and 3000

int top_position = 0;
int bottom_position = 3000;
int middle_position = bottom_position + (bottom_position - top_position)/2;

missdrew:

int top_position = 0;

int bottom_position = 3000;
int middle_position = (bottom_position - top_position)/2;

Hm /2
Thanks!

missdrew:

int top_position = 0;

int bottom_position = 3000;
int middle_position = (bottom_position - top_position)/2;

That should be (bottom_position + top_position) / 2.

If you want a version that works correctly in all cases, even when overflow could occur, you have to be a bit more careful, see https://www.youtube.com/watch?v=sBtAGxBh-XI.

Pieter

maker-20_:
Between the positions 0 and 3000

1, 2, 3, 4, ...., 2999 are all "between" 0 and 3000. You need to be more precise about what you want.

maker-20_:
Between the positions 0 and 3000

int top_position = 0;
int bottom_position = 3000;
int middle_position = top_position + random(bottom_position - top_position);

It fits your definition.