0

I am trying to write a for loop in the second version which produce the same result in the original code but i am not sure how to get -400,-200,-400 in sequential order.
original code:

p->m_p[0] = randFloat(-400.0f, 400.0);
p->m_p[1] = randFloat(-200.0f, 200.0);
p->m_p[2] = randFloat(-400.0f, 400.0);

second version:

float x = -800;
float y = 800;

for(int i = 0; i < 4; i++)
{
plNew->m_fPosition[i] = randFloat(x / 2,y / 2); 
}
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97

5 Answers5

2

If you need it to work in C (or in C++ before C++11), this would work:

#define NUMBER_OF_VALUES 3

float bounds[NUMBER_OF_VALUES] = { 400.0f, 200.0f, 400.0f };

for (int i = 0; i < NUMBER_OF_VALUES; i++)
{
  plNew->m_fPosition[i] = randFloat(-bounds[i], bounds[i]); 
}

You can extend this to make NUMBER_OF_VALUES be 4 or a larger number as long as you initialize all the members of bounds[NUMBER_OF_VALUES] with the desired constants. A nice feature of this is that the sequence of constants can be anything you like, not limited to alternating 400, 200, 400 or any other regular sequence.

David K
  • 3,147
  • 2
  • 13
  • 19
  • This is very unmaintainable, suppose now `3` is ok, but in the future _`You can extend this to make NUMBER_OF_VALUES be 4 or a larger number`_ that will be a huge mess to do. – Iharob Al Asimi Jan 09 '15 at 22:50
  • @iharob This pattern never caused me any maintenance headaches in code where I've used it (some of which I've maintained for over a decade). What do you think will happen? – David K Jan 09 '15 at 22:52
  • Sure, it's a matter of taste, I'm not saying it wouldn't work, but it's a very ugly solution, suppose the values are not fixed, for example you want `{300.0f, 150.0f, 300.0f}`. – Iharob Al Asimi Jan 09 '15 at 22:54
  • 1
    @iharob It depends where the values 400, 200, 400 came from in the first place. If their most important property is their _ratios_, then define an array `bounds_ratios` instead of `bounds`, containing perhaps `{2.0f, 1.0f, 2.0f}` and scale each member appropriately when using it. In any case, any solution is only as good as the description of the problem it has to solve. What if next month the series would be extended from `400, 200, 400` to `400, 200, 400, 150, 300, 60, 720`? No problem for me because I made no assumptions about the pattern of the numbers. – David K Jan 09 '15 at 23:00
  • @iharob Thanks. Of course if I knew the sequence would most likely be extended by alternating values 200 and 400, however many are required, I would never do it this way. I would do something more like your answer. – David K Jan 09 '15 at 23:08
1

Something like this?

for (int i = 0; i < 4; i++) {
    float x, y;

    if (i % 2) {
        x = -400.0f;
        y = 400.0f;
    } else {
        x = -200.0f;
        y = 200.0f;
    }

    p->m_p[i] = randFloat(x, y);
}
SirJay
  • 119
  • 4
  • 8
0

What about?

float x = -800;
float y = 800;

for(int i = 0; i < 4; i++)
{
    float z = 2.0 * ((float)((i + 1) % 2 + 1));
    plNew->m_fPosition[i] = randFloat(x / z, y / z); 
}
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
0

I would suggest keeping it simple, using something like the following :

const float arr3[] = {-400.0f, -200.0f, -400.0f};

for(int i = 0; i < 3; i++)
{
    plNew->m_fPosition[i] = arr3[i]; 
}
user3629249
  • 16,402
  • 1
  • 16
  • 17
0
float x = -400;
float y =  400;

for(int i = 0; i < 3; i++)
{
    plNew->m_fPosition[i] = randFloat(x / (1 + (i & 1)), y / (1 + (i & 1))); 
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70