Proton BASIC Compiler - Random Number From 0 to n

  • PicŪ Basic

  • Random Number From m to n

    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.