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.

Setting a variable to a random number between 2 variables

Ok so I thought there was an option for this in the normal event programming but there isn't.
You can set a variable to the value of another, and you can set a variable to a random between 2 constants, but I need to be able to set a variable to a value between 2 other variables.

I'm not going to reveal too much about the system that it's going to be used for, but i'll give a very brief outline of the situation...

variables used...
1 = player input
2 = buffer
3 = upper limit
4 = lower limt

Player enters a number between 1000-10000

There are 10 stages of difficultly

1000 2000 3000 4000 5000 6000 7000 8000 9000 10000

Each stage can change by either + or - of the same value.
For example
1000 = 0 - 2000
2000 = 0 - 4000
3000 = 0 - 6000
4000 = 0 - 8000
5000 = 0 - 10000
6000 = 0 - 12000
7000 = 0 - 14000
8000 = 0 - 16000
9000 = 0 - 18000
10000 = 0 - 20000

So if the player enters 1267, the difference is 267 - 2267
5635 is 635 - 10635

I have already completed the coding to calculate the upper and lower limits of what the player enters, however now I need to be able to set a variable to a random number between the upper and lower limits.

For example, if the user enters 4964
I need to set a variable to a random number between 964 and 8964.
Entering every possible combination is not an option, it would take way too long.

My current thoughts would be to create something like the following:

set variable 2(buffer) to value of variable 1(player input)
if variable 2(buffer) bigger than 1000 & smaller than 1999
set variable 2(buffer) to a random number between 1 - 2999
if variable 2(buffer) is less than variable 4(lower limit of player input(calculated earlier))
set variable 2(buffer) to the value of variable 4(lower limit)
else
if variable 2(buffer) is greater than variable 3(upper limit of player input(calculated earlier))
set variable 2(buffer) to the value of variable 3(upper limit)

This is not what I wanted, but at the moment its the only option I can think of, it sets a random number, and check to makes sure its not lower or higher than the limits set.
This fails in the following method...

Player enters 1936

Limits calculated at 936 to 2936

Random number can be set anywhere from 1 - 2999

So there is an area from 1 - 936 where if the random number is chosen within here, it will be set as 936.

Anyone got any ideas? is it easy to set a variable to a random value between 2 variables in RGSS?


EDIT: Had an idea

I could set a label above random number select then, if the variable chosen is higher or lower than the lmits, I could return to the label, rather than setting it to the lmit.
This is better but still means processing more coding than nessecary, the code is huge already and branches are way off the visable area.
 
All I can think of is to set the variable between 1 or 2, and then give the variables the value you need them to have

for instance: you need variable mode, to be set to a random variable between 655, 233, and 111 (only 1 of those three)

So you do this:

set mode random 0-2

if mode=0 then
mode=655
else
if mode=1 then
mode=233
else
if mode=2 then
mode=111
end
end
end

does this work?
 
Scripting is not necessary.
for any number between 1000 > 1999,
generate a random number between -1000 & 1000
add the random number to the users number.
Viola!
For numbers between 2000 > 2999
your random number is between -2000 & 2000
etc...
only 10 cases.

There is probably a slightly more efficient way using a single random number between -10000 & +10000, and multiplying by the
((users number - (users number MOD 1000)) / 10000), resulting in a number between .1 - 1.0, then adding the result to the users number.
If you decide to event it this way, I want to see it! :)

Be Well
 

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