How to calculate the randomness "15% of the time"?

I am looking for a decent, elegant method for computing this simple logic.
Right now I canโ€™t think of one thing, it twists my head.

I have to perform some action only in 15% of cases.

I'm used to โ€œ50% of the time,โ€ when I just change the milliseconds of the current time and see if it is strange or not, but I donโ€™t think it is elegant.

How would I elegantly calculate 15% of the time? Random Number Generator maybe?
Pseudocode or any language is welcome.

I hope this is not subjective, as I am looking for the โ€œsmartestโ€ short way to do this.

Thanks.

+4
source share
8 answers

Solution 1 (dual)

  • get a random double between 0 and 1 (any language you use should have such a function)
  • perform an action only if it is less than 0.15

Solution 2 (int)

You can also achieve this by creating a random int and see if it can be divided into 6 or 7. UPDATE -> This is not optimal.

+3
source

You can create a random number from 0 to 99 and check if it is not less than 15:

if (rnd.Next(100) < 15) ... 

You can also reduce the numbers, since 15/100 is the same as 3/20:

 if (rnd.Next(20) < 3) ... 
+2
source

Just use PRNG. As always, this is a performance tradeoff with precision. I think doing your own direct time is a waste of time (pun intended). You will likely get bias effects, even worse than the mileage of a linear congruent generator.

In Java, I would use nextInt :

 myRNG.nextInt(100) < 15 

Or (basically) is equivalent to:

 myRNG.nextInt(20) < 3 

There is a way to get a random integer in other languages โ€‹โ€‹(in fact, these are different ways, depending on how accurate they are).

+1
source

The random number generator will give you the best chance. Generate a random value between 0 and 1, test for <0.15.

The use of such time is not accidental, since it depends on the processing time. If the task requires less than 1 millisecond, the next random selection will be the same.

However, if you want to use the millisecond method, do milliseconds % 20 < 3 .

+1
source

Using modular arithmetic, you can easily do something every Xth run this way

 (6 will give you ruthly 15% 

if (microtime ()% 6 ===) do it

other:

 if(rand(0,1) >= 0.15) do it 
0
source
 boolean array[100] = {true:first 15, false:rest}; shuffle(array); while(array.size > 0) { // pop first element of the array. if(element == true) do_action(); else do_something_else(); } // redo the whole thing again when no elements are left. 
0
source

This quote is from a great article on how to use a random number generator:

Note. Do not use

  y = rand() % M; 

as it focuses on the low bits of Rand (). For linear congruent random numerical generators that rand () often has, lower bytes are much less random than higher bytes. in fact, the lowest bit of the loop is between 0 and 1. Thus, rand () can cycle between even and odd (try). Note. Rand () does not have to be a linear congruent random number generator. It is perfectly acceptable for this to be something better that this problem does not have.

and contains formulas and pseudo codes for

  • r = [0,1) = {r: 0 <= r <1} real <= x <M} real
  • y = [0, M) = {y: 0 <= y <M} integer
  • z = [1, M] = {z: 1 <= z <= M} integer
0
source

Here's one approach that combines randomness and guaranteeing that you end up with a positive result in a predictable range:

You have a target (15 in your case), a counter (initialized to 0) and a flag (initialized to false).

 Accept a request. If the counter is 15, reset the counter and the flag. If the flag is true, return negative outcome. Get a random true or false based on one of the methods described in other answers, but use a probability of 1/(15-counter). Increment counter If result is true, set flag to true and return a positive outcome. Else return a negative outcome. Accept next request 

This means that the first request has a probability of returning 1/15 of a positive result, but on the 15th request, if a positive result has not been returned, the probability is 1/1 of a positive result.

0
source

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


All Articles