Generating a random number x from m to n in one line of code
Level: Beginner
x = (Random // (n - m + 1)) + m
This performs a modulus divide on the random number (equivalent to Mod in some PC languages), thus returning the remainder. This is then added to an offset value m.
If you divide any number x by i, you will always get a remainder of between 0 and (i - 1), therefore, a large random number can always easily be scaled to a small number from an offset m to a chosen value n.


Menu
Recent Articles


Using PDS with SPI GLCD based on ST7565R Controller
Graphic LCDs based on the ST7565 are cheaper then GLCDs with other controllers. SPI requires only four pins. If the circuit