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.
  • Recent Activity

    Stephen Moss-711

    error in lcd 16x2

    Thread Starter: JosÚ

    best regards friends Well, I designed a code with pic 18f2550, this code is for measuring resistors and capacitors, but when simulating in...

    Stephen Moss Today, 14:05 Go to last post