Difference between revisions of "Get random"
From Unofficial QEdit Wiki Guide
Japanaman2 (Talk | contribs) |
|||
(One intermediate revision by the same user not shown) | |||
Line 7: | Line 7: | ||
==Random number generator== | ==Random number generator== | ||
− | Random number generator generates a random number | + | 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== | ==Use== | ||
Line 14: | Line 15: | ||
==Example== | ==Example== | ||
− | |||
<span style='font-size:12px;font-family:courier'> | <span style='font-size:12px;font-family:courier'> | ||
− | + | <span style='color:orange'>// Generates a uniform random number between 100 and 150, inclusive </span> | |
− | + | <span style='color:green'>leti</span> r120, 0 <span style='color:orange'>// First register should always be 0 unless you want a biased result </span> | |
− | + | <span style='color:green'>leti</span> r121, 51 <span style='color:orange'>// Second register = size of the range (51 possible values to cover 100 to 150) </span> | |
− | + | <span style='color:blue'>get_random</span> r120, r122 <span style='color:orange'>// Set r122 to a random number in the range 0-50 </span> | |
− | + | <span style='color:green'>addi</span> r122, 100 <span style='color:orange'>// Move the random number into the range 100-150 </span> | |
− | + | ||
− | + | ||
− | + | ||
− | <span style='color:green'> | + | |
− | <span style='color:green'> | + | |
− | + | ||
− | <span style='color: | + | |
− | + | ||
− | + | ||
− | + | ||
− | <span style='color:green'> | + | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
</span> | </span> | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− |
Latest revision as of 05:44, 28 February 2025
Contents
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