K
K
Klef2012-12-26 16:41:30
Probability theory
Klef, 2012-12-26 16:41:30

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:
graph.png
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

6 answer(s)
A
agmt, 2012-12-26
@agmt

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))".

M
MiXei4, 2012-12-26
@MiXei4

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.

K
Klef, 2012-12-26
@Klef

because the answer was given within the first half hour, I was once again convinced that I was a bad mathematician

N
NiGHt_LEshiY, 2012-12-26
@NiGHt_LEshiY

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?

N
nonbody, 2017-03-08
@nonbody

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

L
livestd, 2017-05-25
@livestd

var getr=function (len){
sqr=Math.pow(len,2);
rnd=Math.random() * (sqr);
sqrt=Math.sqrt(rnd);
frnd=Math.floor(sqrt);
return frnd;
}
var arr=[0,0,0,0,0,0,0,0,0,0,0,0];
for (var i=0;i<=10000;i++){
arr[getr(12)]+=1;
if (i>=10000)
console.log(arr);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question