• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

C++ how to give "array -1" a value

reisbro

Member
Joined
May 15, 2017
Messages
94
Solutions
1
Reaction score
7
Simple question, I have this:
double percentMax[4] = {0.25, 0.40, 0.80, 1.50};

and I want to make a uniform_random(x, y) where:
x = percentMax[i-1]
y = percentMax[ii] <- I put double i here otherwise the forum would interpret it as italic

but If my i = 0, there is no i = -1 since 0 is the first space of the array.
I managed to do what I want but the code got pretty big, and I am pretty sure there is a simpler way hehe
 
Last edited:
perfect! I had to do a quick study because I never used ? before, but I think I understand it now.

if i > 0 i, else 0 right?
right, but the way i think while writing it:
boolean_condition ? if_true_condition : if_false_condition
 
Oh I think that if I use it like this, when i is not >0 then x = percentMax[0] and not x = 0... That means x = 0.25
However I need x = 0 if i = 0
 
maybe uniform_random (0 ? i=0 : percentMax[i-1], percentMax[ii])?

again, the [ii] <- double i is just so the forum doesn't think I want to write italic haha
 
Just push whatever you want to be at -1 to 0, pushing the rest of values further? And start your loop from 1 (so you get x = 0, y = 1). Like this:

C++:
double percentMax[5] = {0.0, 0.25, 0.40, 0.80, 1.50};

for(int i = 1; i < 5; i++) {
   uniform_random(parcentMax[i-1], percentMax[i]);
}
 
I mean std::max
for sure could work, but this is a simple thing, and usually compiler optimizer can't inline STL functions, due all polymorphism c++ thing, functions that are not inlined doesn't make cpu quite happy, tho i'm not sure about ::max
 
Just push whatever you want to be at -1 to 0, pushing the rest of values further? And start your loop from 1 (so you get x = 0, y = 1). Like this:

C++:
double percentMax[5] = {0.0, 0.25, 0.40, 0.80, 1.50};

for(int i = 1; i < 5; i++) {
   uniform_random(parcentMax[i-1], percentMax[i]);
}
wow this is the simplest solution ever haha
 
Back
Top