Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

(Solved) Random Number Between Variables

Kipe

Member

I haven't found a way to do this, maybe there is a simple script command for it.

RMXP Has the ability to choose a random number between two set numbers. Unfortunately, I need to be able to choose a random number between two variable values.

For Example:
Variable 1's Value = 0
Variable 2's value = 100
I need to get Variable 3's Value = a Random between Variable 1's Value and Variable 2's Value.

In other words, I need to be able to get variable 3 to choose a random number between 0 and 100.

Any help would be appreciated.


---Solved---
Thank you Yeyinde!

For those interested, this is how to do it:

You simply place the code below within the [Script] event command.
Code:
max = $game_variables[0002]

min = $game_variables[0001]

result = rand(max - min) + min

$game_variables[0003] = result

You will replace the numbers in the [Brackets] with the variables in which you wish to determine a random number between. max = the bigger number in the sequence, and min should be the smaller number. In this example...
max = $game_variables[0002] should have a higher value than...
min = $game_variables[0001]
To store the final result into another variable for use later on, you simply replace the numbers in the [Brackets] in this command:
$game_variables[0003] = result
to the number of the variable that you wish to store the result to.

You do not need to modify this line of code:
result = rand(max - min) + min

Now variable[0003] = a random number between variable[0001] and variable[0002]

Good luck!
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top