All Questions

Community

D
Gonsen
posted to: Introduction

minor rand_range issue all over the Simulation game course

All over the course code, the following line of code is used:

rand_range(0, REGIONS.size() - 1)

The problem is that rand_range returns a float, and for example with rand_range(1, 10) the probability to get exactly 10 must be very low, the documentation doesn't specify if the last number is inclusive or not, if it's not, we can get 9.9999 but not 10.

This makes entities that have multiple images to always or almost always skip the last one. Trees, branches, boulders and stones.

In my project I just removed the -1 and risking a possibly very low probability to crash. But I'm sure there are better solutions.

For example,RandomNumberGenerator has a randi_range method for integers and documentation explicitly says the last number it's inclusive.

  • Razoric replied

    That was an oversight. I'll issue a fix. The other option would be randi() % REGIONS.size()