Get random

From Unofficial QEdit Wiki Guide
Revision as of 22:43, 10 June 2022 by Ender (Talk | contribs)

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

How the function appears in the script


100:     leti R121, 00002710 //Makes register 121 equal 00002710. (10000 dec.)
         call 230 //Calls function 230.
         ret 
230:     leti R120, 00000000 //Makes register 120 equal 00000000.
         sync  //Waits 1 frame.
         gettime R123 //Stores the current number of frames in the day in register 123.
         clear R122 //Makes register 122 equal 00000000.
         get_random R120, R122 //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.
         sub R123, R122 //Subtracts the value of register 122 from the value of register 123 and stores the result in register 123.
         let R122, R123 //Makes the value of register 122 equal the value of register 123.
         sub R121, R120 //Subtracts the value of register 120 from the value of register 121 and stores the result in register 121.
         div R123, R121 //Divides the value of register 123 by the value of register 121 and stores the result in register 123.
         mul R123, R121 //Multiplies the value of register 123 by the value of register 121 and stores the result in register 123.
         sub R122, R123 //Subtracts the value of register 123 from the value of register 122 and stores the result in register 122.
         add R122, R120 //Adds the value of register 120 to the value of register 122 and stores the result in register 122.
         addi R122, 00000001 //Adds 00000001 to the value of register 122.
         clear R120 //Makes register 120 equal 00000000.
         clear R121 //Makes register 121 equal 00000000.
         clear R123 //Makes register 123 equal 00000000.
         ret 

Also see

leti, call, ret, sync,

gettime, clear, sub, let,

div, mul, add, addi