B
B
BonBon Slick2017-05-04 19:13:28
PHP
BonBon Slick, 2017-05-04 19:13:28

Expose the probability of falling TRUE in an array of 100 elements with FALSE?

I tried to google, they use the mt_rand functions everywhere and it’s not clear what is involved with them. I think that in my case, since I only need to return TRUE or FALSE , it’s easier to take an array of 100 elements, we set the probability of TRUE falling out to be 30%, which means that in the array we have 30 elements with the TRUE value. We shuffle the array several times, and then we get a random index from there, look at the value, voila.
However, I'm wondering if there is anything else in this spirit that is simple and reliable? Will the result be affected if the array has 10, 1,000, or 1,000,000 elements?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
GavriKos, 2017-05-04
@BonBonSlick

random index

So you know how to take randomly. And in range too.
Then - throw a random from 0 to 100. If <30 - then True, otherwise - false.
There is nowhere more reliable.
The array is redundant - this is a waste of memory that no one needs.

D
Daemon23RUS, 2017-05-04
@Daemon23RUS

get a random index from there

How good is the random index? This will affect the quality of the sample.
Wouldn’t it be easier to immediately compare the random index with the given value

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question