Get random

From Unofficial QEdit Wiki Guide
Revision as of 05:44, 28 February 2025 by Ender (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Syntax

Syntax: get_random register 1, register 2

  • register 1 = Start of continuous registers to determine the lowest and highest possible values.
  • register 2 = Register that will contain your returned random number.

Random number generator

Random number generator generates a random number using a specified minimum and maximum. The minimum value is inclusive and the maximum value is exclusive.

It's recommended to adjust the minimum and maximum values to ensure the minimum value is always 0. The reason is that this opcode rolls a random number in range [0, maximum). Then if the rolled number is lower than the minimum, the output is set to the minimum. This means using min = 98, max = 100 would return a value of 98 approximately 99% of the time, instead of the expected 50-50 split.

Use

Used to generate a random number.

Example


// Generates a uniform random number between 100 and 150, inclusive 
leti        r120, 0     // First register should always be 0 unless you want a biased result 
leti        r121, 51    // Second register = size of the range (51 possible values to cover 100 to 150) 
get_random  r120, r122  // Set r122 to a random number in the range 0-50 
addi        r122, 100   // Move the random number into the range 100-150