c# - Dividing by Random.next always results in 0? -
this 1 puzzling me. writing damage algorithm random variation. when calculate variation looks like.
random random = new random(); double variation = random.next(85, 115) / 100; double damage = restofalgorithm * variation;
when that, variation outputs 0. however, if below, output expected result.
random random = new random(); double variation = random.next(85, 115); double damage = restofalgorithm * (variation / 100);
why happen?
divide double:
double variation = random.next(85, 115) / 100.0;
or
double variation = random.next(85, 115) / (double)100;
otherwise you'll doing integer arithmetic (since random.next
returns integer , 100 integer).
i consider best practice know types working , cast type desired. more necessary, compiler implicitly convert values. explicit casts intentions visible looking @ code later.
double variation = (double)random.next(85, 115) / 100d;
Comments
Post a Comment