How to create probability by a certain percentage?

I am trying to create a percentage probability for the game. For instance. if the item has a 45% chance of a critical hit, this should mean that 45 out of 100 hits will be critical.

At first I tried using a simple solution:

R = new Random(); int C = R.Next(1, 101); if (C <= ProbabilityPercent) DoSomething() 

But in 100 iterations with a probability of, for example, 48%, it gives 40-52 out of 100. The same goes for 49, 50, 51. Thus, there is no difference between these “percentages”.

The question is how to set the percentage, for example. 50, and get strictly 50 out of 100 with random? This is a very important thing for the probability of finding rare items with the ability to increase the chance of finding an item. Thus, the 1% buff will be sensitive, because now it is not.

Sorry for my bad english.

+4
source share
4 answers

You only need to think about uniform distribution over the repeated rolls.

You cannot watch more than 100 rolls because enforcing exactly 45 will not be random. Typically, these videos should show a "lack of memory." For example, if you roll dice in search of 6, you have a 1-in-6 chance. If you throw it 5 times and don’t get six - then: the chance to get 6 in the next roll is not 1. It is still 1 in 6. So you can only see how well it met your expectation when a statistically large amount is amortized events ... 100,000 say.

Basically: your current code is fine. If the user knows (because they scored 55 times without critical) that the next 45 hits must be critical, then he is no longer random, and they can play the system.

Also; 45% chance of a critical hit seems a little high, p

+8
source

I am trying to create a percentage probability for the game. For instance. if the item has a 45% chance of a critical hit, this means that 45 out of 100 hits will be critical.

No, it is not. You have completely missed the concept of probability. You do not want a “percentage probability”; you want a “percentage random distribution of 100 samples.”

What you need is a "bag of events", 45 of which are "critical" and 55 of them are "non-critical". After you select an event from the bag, you will only have the rest of the events to choose next time.

You can simulate it as follows:

 Initialize two integer variables Crytical and NonCrytical so that they sum exactly 100 according to the desired percetnages. Get a random value from 1 to Crytical+NonCrytical If the random value is less than the value of Crytical, let you hit be crytical and: Crytical = Crytical -1 Else, let your hit be non-crytical NonCrytical = NonCrytical-1 End If Repeat until Crytical+NonCrytical = 0 
+4
source

Since I am not an expert in C #, I will use the C ++ function for convenience, but it is still applicable for any language.

rand () is a random number generator.

 //33.34% double probability = 0.3334; //divide to get a number between 0 and 1 double result = rand() / RAND_MAX; if(result < probability) //do something 

I used this method to create very large percolated meshes, and it works great for precision values.

+2
source

The fact is that with Random you can initialize this class only once.

This is because Random uses system time as a seed to generate random numbers. If your loop is very fast, it may happen that several random instances use the same seed and thus generate the same numbers.

Check the generated numbers if you suspect this is happening.

In addition, the inherent Randomness is that it will not give you accurate results. This means that even with a 50/50 probability, it may happen that a sequence of 100 “heads” or “tails” gives “heads” 100 times.

The only thing you can do is create a random instance once and live with the results; otherwise you should not use Random.

0
source

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


All Articles