Jump to content

Need scripting help: Generating a random number.


Guest Messenjah

Recommended Posts

Guest Messenjah

I'm trying to understand how GetRandomPercent works exactly.

 

 

How can I generate a number between 1 and 4 without using NVSE. I would really rather use getrandompercent if possible.

 

Link to comment
Share on other sites

without using NVSE

 

wiki:

To generate a random number between min and max:
set randVal to min + GetRandomPercent * (max-min+1) / 100

But the truth is - NVSE is faster and lighter:

set iFloat to Rand min max

Edited by Xilandro
Link to comment
Share on other sites

Guest Messenjah

True, but the reason I don't want to include NVSE is for those players who may not download NVSE.

 

I saw that on the Wiki but I'm trying to get a better understanding of how that works?

 

If for example, I want to generate a number between 1 and 4... would I write it like:

set randVal to min + GetRandomPercent * (4-0+1)/100

or

set randVal  to 1 + GetRandomPercent * (4-0+1)/100

or

set randVal to min + GetRandomPercent * (4-1)

or

randVal to  1 + GetRandomPercent * (4-0+1) /100

Not sure how this works?

Link to comment
Share on other sites

There's no reason to use Rand from NVSE, unless you're already going to use NVSE for some other reason. That would be an unnecessary dependency.

The two examples listed on the wiki page are ready as is, you simply put in the desired min/max values and that's it. However, it's beneficial to actually understand how it works, and it's quite easy, given the simplicity of the math.

GetRandomPercent returns an integer between 0 and 99 inclusive. The simplest way to generate a number between 1 and 4 would be this:

short rnd
set rnd to 1 + GetRandomPercent * 0.04
  • Multiply the function call by 0.04 (equivalent to 4 / 100, dividing your max value by 100) and the range becomes 0 - 3.96.
  • Add 1 and the range becomes 1 - 4.96. (Remember the order of operations, multiplication happens before addition.)
  • And of course, by saving it into a short (integer) variable, any fractions are shaved off, making the resulting range 1 - 4.

If you want it to be 0 - 4 instead, then do this:

short rnd
set rnd to GetRandomPercent * 0.05
  • Because the function already has the desired minimum value of 0, we don't add 1. But this means that the range would be 0 - 3.96, which is too small, so we need to multiply by a larger number.
  • Multiply the function call by 0.05 (equivalent to 5 / 100, having added 1 to the desired max value) and the range becomes 0 - 4.95.
  • Save to a short variable and the fraction is shaved off, resulting in a 0 - 4 range.

Now let's change things up a bit. Here's a 2 - 7 range:

short rnd
set rnd to 2 + GetRandomPercent * 0.06
  • I arrived at the number 0.06 by subtracting the min value from the max value (7 - 2 = 5) and by adding one because otherwise the max value would be too small (5 + 1 = 6). The reason for subtracting the min value from the max value, is that the min value will be added later and used as an offset. Divide by hundred and you get the result (6 / 100 = 0.06). Multiply with the function call and it gives a range of 0 - 5.94.
  • Adding 2 results in a range of 2 - 7.94. Save it to a short variable and it becomes 2 - 7.
  • This is the same procedure as the first example. The only difference is that the subtraction of the min value (1) and the addition to get the proper range (also 1) balanced each other out, so they could be safely ignored.

I hope this clears things up. :smile:

Link to comment
Share on other sites

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...