... i have again to start reading the book from page 1
Dear Community,
Was looking through the reference and many example files, but did not get the solution for my problem. - I want to display several values on a TFT display, to test the code and look i want to use random numbers. Fortunately the "hard" part, displaying the data, works fine but i got stuck completely with the Syntax or his sister or her brother (however ...) for simple calculations ...
1.)
This was the 1st attempt which seemed to me obvious to work :
val = int((random(maxval) / maxval) * 100);
This expression gives always 0 ... do i want to do things which are impossible or am i using the impossible way ?
2.)
I tried now to break up the math with single line calculations and display the calculation progress in the serial monitor. To show you what i see i made a screendshot of the code and the serial datagram (val, rndnum = int ; valcalc1, valcalc2 = float ; maxval = 1000) :
Maybe i have to sleep some more hours and i will see the issue, my answer is still 42, but this doesnt help :. ... maybe you know the answer ? - Any hint is welcome !
int maxval = 1000; //whatever you want maxval to be
int rndnum = random(maxval);
float valcal1 = rndnum/maxval;
float valcal2 = valcal1 * 100;
int val = int(valcal2); //assuming int() is a function that rounds valcal2 to the nearest integer...
Anything times zero is zero - that's not syntax, that's arithmetic.
Well, i'm aware of that fact, but for my understanding the result of the term "random(maxval)" is a random value with its maximum at "maxval". The term "(random(maxval) / maxval)" could be zero but will be almost a something between 0 and 1 ... thats why i think of a boring syntax error ...
@Qdeathstar:
That's exactly how i defined all of the var's
you can combine all "constant" variables - especially if maxval is defined const, a max value typically is constant or?
const int maxval = 1000;
float rndnum = random(maxval) * (100.0/maxval); Â // combine the constants so the compiler can optimize.
int val = round(rndnum ); // rounding iso truncating?
Looking at the semantics of the code, you seem to want a random integer between 0..100 (percentage?)
is that right?
Thx Mike, that really helped me now, it works ! - [/SyntaxError]
@robtillaart :
Yes, i need a percentage value as an int and maxval is a constant. - Many thanks for your proposal, i think thats the way how i wanted it myself