algorithm - Create a Random Number Generator method from A given Random number generator method with different range -
i want make random number generator using given random number generator method.
given function randk() return random number 1 k, create function randn() return random number 1 n equal probability.
where k can be both less or more n (k<n or k>n).
i understand if (k > n) can keep calling randk() till return (value <= n).
- but
equal probabilityproperty maintained.? - also case when
(k<n)?
`
1) yes, numbers between 1 k have same probability appear, , probability 1 / k (not 1 / n), equally probable (also, if k - n high, number of calls f() inside fn() may increase).
2) create x-tuple of values (f(), f(), ..., f()), x smaller integer satisfies following in-equation: kx >= n
we care x such kx > n, turn problem case 1 (where k > n). if generated x-tuple outside first n tuples, start again.
example: given f() returns number in [1, 5] (equally probable), want create fn() returns number in [1, 10] (also equally probable).
the first integer x satisfies 5x >= 10 x = 2, (25 >= 10).
then x-tuple of form (f(), f()) (just tuple), call function f() twice , apply case 1:
(1,1) = 1, (1,2) = 2, (1,3) = 3, (1,4) = 4, (1,5) = 5, (2,1) = 6, (2,2) = 7, (2,3) = 8, (2,4) = 9 , (2,5) = 10 if 1 of other 15 tuples [(3,1),(3,2),(3,3),(3,4),(3,5),(4,1),(4,2),(4,3),(4,4),(4,5),(5,1),(5,2),(5,3),(5,4),(5,5)] repeat algorithm again until 1 first 10 tuples.
the 10 tuples have same probability of 1/25 appear.
Comments
Post a Comment