Answer the question
In order to leave comments, you need to log in
Rand with different probability?
Hello, my favorite hub "abnormal programming"
Now I'll throw you a puzzle on your ingenuity.
Condition: there is a sequence, from 1 to 100, you must randomly select a number from this sequence, but so that 1 has the lowest choice priority and 100 has the highest.
I have a solution (even two, one programmer (I found it myself), the second mathematical (it was already suggested by my comrades)
Here is a picture demonstrating:
by X sequence from 1 to 100
by Y chance of falling out
white - pure random
red - prog solution
green - mathematical
gray - zero, for the purity of the standard
As it turned out, when I attracted familiar developers to solve this problem, it became too tough for many.
All suggestions can be added to the schedule. for check.
Tomorrow evening I will show what I have for options.
upd: added the time spent on the miscalculation.
Answer the question
In order to leave comments, you need to log in
If the probability density increases uniformly, then the distribution function increases along the parabola. So in the end, the desired number can be obtained using the formula "1 + sqrt (rnd (10000))".
We generate a number from 1 to the sum of an arithmetic progression from 1 to 100 = 5050.
We get x. Next, we consider at what minimum number y the number x will be less than the sum of the arithmetic progression from 1 to y.
The meaning is this - a 1 falls out - the answer is 1, a 2 or 3 falls out - the answer is 2, a 4, 5, 6 falls out - the answer is 3, etc.
because the answer was given within the first half hour, I was once again convinced that I was a bad mathematician
The first thing that came to mind is ideone.com/HO6sF0
My knowledge of mathematics and algorithms is clearly not enough, but ... is this at least approximately the right line of thought?
for($i=0; $i<1000; $i++){
$chance = 30;
if (rand(1,100)<=$chance){
echo "yeah\r\n";
}else{
echo "no chance\r\n";
}
}
Here is the simplest way
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question