probability

Unbiased random number generator using a biased one

混江龙づ霸主 提交于 2019-11-28 23:26:25
You have a biased random number generator that produces a 1 with a probability p and 0 with a probability (1-p). You do not know the value of p. Using this make an unbiased random number generator which produces 1 with a probability 0.5 and 0 with a probability 0.5. Note : this problem is an exercise problem from Introduction to Algorithms by Cormen, Leiserson, Rivest, Stein.(clrs) pau.estalella The events (p)(1-p) and (1-p)(p) are equiprobable. Taking them as 0 and 1 respectively and discarding the other two pairs of results you get an unbiased random generator. In code this is done as easy

Constructing a multi-order Markov chain transition matrix in Matlab

此生再无相见时 提交于 2019-11-28 23:21:29
问题 A first-order transition matrix of 6 states can be constructed very elegantly as follows x = [1 6 1 6 4 4 4 3 1 2 2 3 4 5 4 5 2 6 2 6 2 6]; % the Markov chain tm = full(sparse(x(1:end-1),x(2:end),1)) % the transition matrix. So here is my problem, how do you construct a second-order transition matrix elegantly? The solution I came up with is as follows [si sj] = ndgrid(1:6); s2 = [si(:) sj(:)]; % combinations for 2 contiguous states tm2 = zeros([numel(si),6]); % initialize transition matrix

Fast weighted random selection from very large set of values

元气小坏坏 提交于 2019-11-28 22:56:16
问题 I'm currently working on a problem that requires the random selection of an element from a set. Each of the elements has a weight(selection probability) associated with it. My problem is that for sets with a small number of elements say 5-10, the complexity (running time) of the solution I was is acceptable, however as the number of elements increases say for 1K or 10K etc, the running time becomes unacceptable. My current strategy is: Select random value X with range [0,1) Iterate elements

Distributed probability random number generator

孤街醉人 提交于 2019-11-28 21:16:49
I want to generate a number based on a distributed probability. For example, just say there are the following occurences of each numbers: Number| Count 1 | 150 2 | 40 3 | 15 4 | 3 with a total of (150+40+15+3) = 208 then the probability of a 1 is 150/208= 0.72 and the probability of a 2 is 40/208 = 0.192 How do I make a random number generator that returns be numbers based on this probability distribution? I'm happy for this to be based on a static, hardcoded set for now but I eventually want it to derive the probability distribution from a database query. I've seen similar examples like this

Minimizing NExpectation for a custom distribution in Mathematica

风格不统一 提交于 2019-11-28 20:41:37
问题 This relates to an earlier question from back in June: Calculating expectation for a custom distribution in Mathematica I have a custom mixed distribution defined using a second custom distribution following along the lines discussed by @Sasha in a number of answers over the past year. Code defining the distributions follows: nDist /: CharacteristicFunction[nDist[a_, b_, m_, s_], t_] := (a b E^(I m t - (s^2 t^2)/2))/((I a + t) (-I b + t)); nDist /: PDF[nDist[a_, b_, m_, s_], x_] := (1/(2*(a +

How do I generate points that match a histogram?

蓝咒 提交于 2019-11-28 19:43:17
I am working on a simulation system. I will soon have experimental data (histograms) for the real-world distribution of values for several simulation inputs. When the simulation runs, I would like to be able to produce random values that match the measured distribution. I'd prefer to do this without storing the original histograms. What are some good ways of Mapping a histogram to a set of parameters representing the distribution? Generating values that based on those parameters at runtime? EDIT: The input data are event durations for several different types of events. I expect that different

How to incrementally sample without replacement?

一曲冷凌霜 提交于 2019-11-28 18:21:44
Python has my_sample = random.sample(range(100), 10) to randomly sample without replacement from [0, 100) . Suppose I have sampled n such numbers and now I want to sample one more without replacement (without including any of the previously sampled n ), how to do so super efficiently? update: changed from "reasonably efficiently" to "super efficiently" (but ignoring constant factors) Chronial Note to readers from OP: Please consider looking at the originally accepted answer to understand the logic, and then understand this answer. Aaaaaand for completeness sake: This is the concept of

How to generate random numbers biased towards one value in a range?

烈酒焚心 提交于 2019-11-28 18:19:49
Say, if I wanted to generate an unbiased random number between min and max , I'd do: var rand = function(min, max) { return Math.floor(Math.random() * (max - min + 1)) + min; }; But what if I want to generate a random number between min and max but more biased towards a value N between min and max to a degree D ? It's best to illustrate it with a probability curve: Here is one way: Get a random number in the min-max range Get a random normalized mix value Mix random with bias based on random mix Ie., in pseudo: Variables: min = 0 max = 100 bias = 67 (N) influence = 1 (D) [0.0, 1.0] Formula:

Probability in Java

人盡茶涼 提交于 2019-11-28 17:28:16
I was curious to know, how do I implement probability in Java? For example, if the chances of a variable showing is 1/25, then how would I implement that? Or any other probability? Please point me in the general direction. You'd use Random to generate a random number, then test it against a literal to match the probability you're trying to achieve. So given: boolean val = new Random().nextInt(25)==0; val will have a 1/25 probability of being true (since nextInt() has an even probability of returning any number starting at 0 and up to, but not including, 25.) You would of course have to import

Game Design/theory, Loot Drop Chance/Spawn Rate

北城余情 提交于 2019-11-28 17:12:25
问题 I have a very specific and long-winded question for you all. This question is both about programming and game-theory. I recently added spawnable ore to my Turn Based Strategy Game: http://imgur.com/gallery/0F5D5Ij (For those of you that look please forgive the development textures). Now, onto the enigma that I have been contemplating. In my game, ore is generated each time a new map is created. 0-8 ore nodes are generated per level-creation. I already have this working; except it only