Random function: higher values ​​appear less often than lower

I have a difficult question that I looked through a couple of times without thinking.

Some background: I create a textual RPG game in which players fight animals / monsters, etc. It works like any other game in which you click several hit points on each other every round.

Problem: I use a random function in php to generate the final hit value, depending on levels, armor, etc. But I would like for higher values ​​(for example, maximum hit) to be displayed less often than lower values.

This is an example graph:

Example graph

How can I reproduce something like this using PHP and a rand function? When you enter rand(1,100) each number has an equal chance of choice.

My idea is this: do a 2nd degree (or quadratic function) and use a random number (x) to calculate.

Will it work as I want?

The question is a bit complicated, please let me know if you need more information and details.

+6
source share
2 answers

Please look at this beautiful article: http://www.redblobgames.com/articles/probability/damage-rolls.html

There are interactive charts that look at dice and the percentage of results. This should be very helpful for you.

Pay attention to this random rolling number:

 roll1 = rollDice(2, 12); roll2 = rollDice(2, 12); damage = min(roll1, roll2); 

This should give you what you are looking for.

+1
source

OK, here is my idea:

Let's say you have an array of elements (a,b,c,d) , and you cannot accidentally select one of them. Doing rand(1,4) to get an index of random elements would mean that all elements have an equal chance of appearing. (25%)

Now suppose we take this array: (a,b,c,d,d) .

Here we still have 4 elements, but not each of them has an equal chance of appearing.

  • a, b, c: 20%
  • d: 40%

Or take this array:

(1,2,3,...,97,97,97,98,98,98,99,99,99,100,100,100,100)

Hint: Thus, you not only reject the random number generation algorithm, but actually set the desired probability of each of them (or a range of numbers).


So how would I do this:

If you want numbers from 1 to 100 (with higher numbers appearing more often, get a random number from 1 to 1000 and associate it with a wider range. For example,

 rand = 800-1000 => rand/10 (80->100) rand = 600-800 => rand/9 (66->88) ... 

Or something like that. (You can use any mathematical operation that you represent, modulo or something else ... and play with your algorithm). I hope you understand my idea.

Good luck! :-)

0
source

Source: https://habr.com/ru/post/914216/


All Articles