We start with a variable that holds an integer for the "number of items" that we have somewhere, somehow.
Then, the algorithm is applied: each one of those items have a 70% chance to be destroyed.
If the algorithm was something like round( num_of_items * 0.7 )
, that wouldn't work as expected, for obvious reasons (one would be that items=1
would always return 0
, instead of 70% times 0
and 30% times 1
).
I can't afford calculating a chance of 70% one time for each item, because there might be millions.
Is there any algorithm that could do this with a constant cost?
n
random numbers (0-1), returning how many were less than0.7
. It handlesn=100000000
(one hundred million) in about 20 seconds. What are you trying to use this for? What are your actual performance requirements?