Difference between revisions of "Get random"

From Unofficial QEdit Wiki Guide
Jump to: navigation, search
 
 
(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.
To use the random number generator make a new function. Then leti register 121 to equal the maximum value that you want it to be (note maximum valid value is 10,000 in dec.) Then call function 230 and end with a ret. Your returned random number will be between 1 and the maximum value you stated in register 121, your random number will be stored in register 122.
+
 
 +
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==
==How the function appears in the script==
 
 
  <span style='font-size:12px;font-family:courier'>
 
  <span style='font-size:12px;font-family:courier'>
  <span style='color:blue'>100:    </span><span style='color:green'>leti </span>R121, 00002710 <span style='color:orange'>//Makes register 121 equal 00002710. (10000 dec.)</span>
+
  <span style='color:orange'>// Generates a uniform random number between 100 and 150, inclusive </span>
<span style='color:green'>        call </span>230 <span style='color:orange'>//Calls function 230.</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'>        ret </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'>230:    </span><span style='color:green'>leti </span>R120, 00000000 <span style='color:orange'>//Makes register 120 equal 00000000.</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'>        sync </span> <span style='color:orange'>//Waits 1 frame.</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'>        gettime </span>R123 <span style='color:orange'>//Stores the current number of frames in the day in register 123.</span>
+
<span style='color:green'>        clear </span>R122 <span style='color:orange'>//Makes register 122 equal 00000000.</span>
+
<span style='color:blue'>        get_random </span>R120, R122 <span style='color:orange'>//Gets a random number between the value of register 120 , and the value of register 121. Then stores the returned random number in register 122.</span>
+
  <span style='color:green'>         sub </span>R123, R122 <span style='color:orange'>//Subtracts the value of register 122 from the value of register 123 and stores the result in register 123.</span>
+
  <span style='color:green'>         let </span>R122, R123 <span style='color:orange'>//Makes the value of register 122 equal the value of register 123.</span>
+
<span style='color:green'>        sub </span>R121, R120 <span style='color:orange'>//Subtracts the value of register 120 from the value of register 121 and stores the result in register 121.</span>
+
  <span style='color:green'>         div </span>R123, R121 <span style='color:orange'>//Divides the value of register 123 by the value of register 121 and stores the result in register 123.</span>
+
  <span style='color:green'>        mul </span>R123, R121 <span style='color:orange'>//Multiplies the value of register 123 by the value of register 121 and stores the result in register 123.</span>
+
  <span style='color:green'>        sub </span>R122, R123 <span style='color:orange'>//Subtracts the value of register 123 from the value of register 122 and stores the result in register 122.</span>
+
<span style='color:green'>        add </span>R122, R120 <span style='color:orange'>//Adds the value of register 120 to the value of register 122 and stores the result in register 122.</span>
+
  <span style='color:green'>         addi </span>R122, 00000001 <span style='color:orange'>//Adds 00000001 to the value of register 122.</span>
+
<span style='color:green'>        clear </span>R120 <span style='color:orange'>//Makes register 120 equal 00000000.</span>
+
<span style='color:green'>        clear </span>R121 <span style='color:orange'>//Makes register 121 equal 00000000.</span>
+
<span style='color:green'>        clear </span>R123 <span style='color:orange'>//Makes register 123 equal 00000000.</span>
+
<span style='color:green'>        ret </span>
+
 
  </span>
 
  </span>
 
==Also see==
 
[[leti]], [[call]], [[ret]], [[sync]],
 
 
[[gettime]], [[clear]], [[sub]], [[let]],
 
 
[[div]], [[mul]], [[add]], [[addi]]
 

Latest revision as of 05:44, 28 February 2025

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